Solved

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

Posted on 2011-03-07
18
547 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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
 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
open source backup solution 1 42
awk to variable in bash 2 108
Can't connect to FTP 18 101
Granting root permissions to a user (in Debian Jessie) 17 49
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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