Solved

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

Posted on 2011-03-07
18
556 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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