UBUNTU Script to look in network folder print file that is there and move to other folder

Dear experts,

I want my Ubuntu to look at a network folder (on a windows machine) if there is a file (folder is called "af te drukken")
Ubuntu should pickup that file and print to a network printer (192.168.10.204)
after print it should move to folder "afgedrukt" on the same windows machine..

This script should start automaticaly when ubuntu starts and when the windows machine comes online it should start look for new files,
once every 2 minutes is ok...

is this possible with ubuntu (I run 10.4)

kind regards

Eric
LVL 6
Eric ZwiekhorstSAP Business ConsultantAsked:
Who is Participating?
 
TobiasHolmConnect With a Mentor Commented:
Hi!

First create a Bash script. Check if a file exists with the 'if' command.
[ -d FILE ]      True if FILE exists and is a directory.
[ -f FILE ]            True if FILE exists and is a regular file.
Ref: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

Move the file with the 'mv' command.

Something like this:
#!/bin/sh
                                                               
SRC_DIR="/mnt/WindowsShare/af te drukken/"
DST_DIR="/mnt/WindowsShare/afgedrukt"
mkdir -p $DST_DIR

cd $SRC_DIR
for i in *.pdf
do /usr/bin/lp $i
  if [ $? -eq 0 ]; then
    mv $i $DST_DIR
  fi
done

Open in new window


To run the script every two minutes, use cron.

sudo crontab -e

Open in new window


add a line like this:
0-59/2 * * * /bin/sh /path/to/your/script.sh

Open in new window


Regards, Tobias
0
 
Fabio MarzoccaFreelancerCommented:
Here we give suggestions and find solutions.

We do not write code on order. Your request is not fair.
0
 
Eric ZwiekhorstSAP Business ConsultantAuthor Commented:
That is strange, I work mostly in the Excel part of expert-exchange and we give all the time code to help people...
So in that point of view my question is more then fare, I do not not how to write this code and am asking for help. This is what this site is all about, helping people with IT related questions...

Kind regards

Eric
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Fabio MarzoccaFreelancerCommented:
yes, I can give you guidelines.

You need first to mount the windows share, then send it to the printer with the lpr command, and finally move it with mv.

mount:  gvfs-mount smb://xxx.xxx.xxx.xxx/<foldername>
             cd $HOME/Desktop/<foldername>

print: lpr -H server:port files

move: mv <source> <destination>

0
 
Eric ZwiekhorstSAP Business ConsultantAuthor Commented:
Dear FMarzocca,

what filetype can be printed with the LPR command?

The files present on the windows share will be either .ODT or .ODS or .PDF or .JPG

Thanks for thrying to help

KR

Eric
0
 
Eric ZwiekhorstSAP Business ConsultantAuthor Commented:
Hi Tobias, I will give it a try this evening..

Thank for the code, whith this I can help myself...


Kind regards

Eric
0
 
TobiasHolmCommented:
oh, and don't forget to set "execute" rights on the Bash sh-file!

chmod a+x youscript.sh

Open in new window

0
 
Fabio MarzoccaFreelancerCommented:
To run  the script at every ubuntu start, add it to the file /etc/rc.local
0
 
Eric ZwiekhorstSAP Business ConsultantAuthor Commented:
dear TobiasHolm,

I did try the bash script,
I got the message : printen.sh: command not found
this is the code of the script:

#!/bin/sh                        
SRC_DIR="mnt/mijn map/af te drukken/"
DST_DIR="mnt/mijn map/afgedrukt"
mkdir -p $DST_DIR
cd $SRC_DIR
for i in *.*
do /usr/bin/lp $i
  if [ $? -eq 0 ]; then
    mv $i $DST_DIR
  fi
done


What is wrong? I did set the: chmod a+x printen.sh?

kind regards.

Eric
0
 
TobiasHolmCommented:
Seems like you missed a slash in front of your /mnt in both SRC and DST.

Regards, Tobias
0
 
TobiasHolmCommented:
also, put quote-marks around your CD-line:

cd "$SRC_DIR"

Open in new window

0
 
Eric ZwiekhorstSAP Business ConsultantAuthor Commented:
He Guy's Thanks,

Again this evening I will have a look if it works..

Kind regards

Eric
0
 
Pierre FrançoisSenior consultantCommented:
@Zwiekhorst: If you get the message : printen.sh: command not found, it means that the shell didn't find your script. If your current directory is the same as the directory where the script is saved, you have to issue ./printen.sh...
0
 
Pierre FrançoisSenior consultantCommented:
@Zwiekhorst: I don't know if I made myself clear: put a dot and a slash before printen.sh
./printen.sh

Open in new window

0
 
Eric ZwiekhorstSAP Business ConsultantAuthor Commented:
Ah, that I did not do...

Thanks will sure try it with the ./ before printen.sh

Do I have to put this in front when entering it into crontab?

Kind regards

Eric
0
 
TobiasHolmCommented:
>Do I have to put this in front when entering it into crontab?

No, use the full path in your cron file like in the example:

0-59/2 * * * /bin/sh /path/to/script/printen.sh

Open in new window

0
 
Eric ZwiekhorstSAP Business ConsultantAuthor Commented:
HI experts,

There seems to be a problem with the windows share?

when looking into MNT I can't detect anything, the folder is empty?

when I did try to run the printen.sh (and it ran thanks to you experts..) it started printing everything on my desktop..

Could not find the windows share..

kr

Eric
0
 
TobiasHolmCommented:
Can you run:

ls /mnt/mijn\ map

Open in new window


Do you see your folders?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.