Solved

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

Posted on 2011-03-07
18
542 Views
Last Modified: 2013-12-15
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
0
Comment
Question by:Eric Zwiekhorst
  • 7
  • 6
  • 3
  • +1
18 Comments
 
LVL 14

Expert Comment

by:fmarzocca
ID: 35055091
Here we give suggestions and find solutions.

We do not write code on order. Your request is not fair.
0
 
LVL 6

Author Comment

by:Eric Zwiekhorst
ID: 35055226
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
 
LVL 14

Expert Comment

by:fmarzocca
ID: 35055655
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
 
LVL 6

Author Comment

by:Eric Zwiekhorst
ID: 35055699
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
 
LVL 18

Accepted Solution

by:
TobiasHolm earned 500 total points
ID: 35056218
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
 
LVL 6

Author Comment

by:Eric Zwiekhorst
ID: 35056438
Hi Tobias, I will give it a try this evening..

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


Kind regards

Eric
0
 
LVL 18

Expert Comment

by:TobiasHolm
ID: 35058165
oh, and don't forget to set "execute" rights on the Bash sh-file!

chmod a+x youscript.sh

Open in new window

0
 
LVL 14

Expert Comment

by:fmarzocca
ID: 35058884
To run  the script at every ubuntu start, add it to the file /etc/rc.local
0
 
LVL 6

Author Comment

by:Eric Zwiekhorst
ID: 35061652
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 18

Expert Comment

by:TobiasHolm
ID: 35067705
Seems like you missed a slash in front of your /mnt in both SRC and DST.

Regards, Tobias
0
 
LVL 18

Expert Comment

by:TobiasHolm
ID: 35067866
also, put quote-marks around your CD-line:

cd "$SRC_DIR"

Open in new window

0
 
LVL 6

Author Comment

by:Eric Zwiekhorst
ID: 35068075
He Guy's Thanks,

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

Kind regards

Eric
0
 
LVL 10

Expert Comment

by:pfrancois
ID: 35068915
@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
 
LVL 10

Expert Comment

by:pfrancois
ID: 35068925
@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
 
LVL 6

Author Comment

by:Eric Zwiekhorst
ID: 35069072
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
 
LVL 18

Expert Comment

by:TobiasHolm
ID: 35069127
>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
 
LVL 6

Author Comment

by:Eric Zwiekhorst
ID: 35081677
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
 
LVL 18

Expert Comment

by:TobiasHolm
ID: 35081709
Can you run:

ls /mnt/mijn\ map

Open in new window


Do you see your folders?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now