Link to home
Start Free TrialLog in
Avatar of T0PS3O
T0PS3O

asked on

Multiple Subsequent Commands with Incrontab or Shell Script

Hi,

I've installed incron and it works with basic event based commands like printing a file.

I want to do two things based on a new file being saved in the watched folder, but am struggling to accomplish this.

Incrontab attempts:

# /var/prn IN_CLOSE_WRITE lp -d cups_printer $@/$#
# /var/prn IN_CLOSE_WRITE mv $@/$# /var/prn/printed
# Second command doesn't work, as incron manual already said you can only do one line per folder/file

# /var/prn IN_CLOSE_WRITE lp -d cups_printer $@/$#; mv $@/$# /var/prn/printed;
# Neither command works

How can we do multiple consecutive commands based on one file event?

If it's just plain impossible, I guess I could pass the $@/$# to a bash script. If that's the only way forward, could you write out the bash script for me? I don't have a lot of experience with that.

On a side-note, can we tell incron to ignore hidden files? When I create a file with vim, it attempts to print the .swp files.

Thanks!
Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

First off, try this:

/var/prn IN_CLOSE_WRITE lp -d cups_printer $@/$# && mv $@/$# /var/prn/printed;

Note "&&" instead of ";"

If that doesn't work, try something like this:

# /var/prn IN_CLOSE_WRITE /home/username/script/do_whatever $@/$#

Then in /home/username/script/do_whatever put:

#!/bin/bash
lp -d cups_printer $*
mv $* /var/prn/printed
Avatar of T0PS3O
T0PS3O

ASKER

Your first suggestion doesn't work.

In /var/log/cron I get this, when creating one new file called test4.txt

Feb  3 09:01:50 vm2263 incrond[27016]: (root) CMD (lp -d cups_printer /var/prn/.test4.txt.swx && mv /var/prn/.test4.txt.swx /var/prn/printed;)
Feb  3 09:01:50 vm2263 incrond[27016]: (root) CMD (lp -d cups_printer /var/prn/.test4.txt.swp && mv /var/prn/.test4.txt.swp /var/prn/printed;)
Feb  3 09:01:57 vm2263 incrond[27016]: (root) CMD (lp -d cups_printer /var/prn/test4.txt && mv /var/prn/test4.txt /var/prn/printed;)
Feb  3 09:01:57 vm2263 incrond[27016]: (root) CMD (lp -d cups_printer /var/prn/.test4.txt.swp && mv /var/prn/.test4.txt.swp /var/prn/printed;)

Nothing prints and nothing gets moved. Narrow it down again to just the printing command, without the ending semicolon by the way, and it prints. As you see, that's done as root.

I then tried the script option, which results in a permission error, even when the incrontab is root's and the script is made by root as is the print job file.

Feb  3 09:26:07 vm2263 incrond[27016]: (root) CMD (/root/printing/printlabel.sh /var/prn/.rootscript.txt.swx)
Feb  3 09:26:07 vm2263 incrond[13416]: cannot exec process: Permission denied
Feb  3 09:26:07 vm2263 incrond[27016]: (root) CMD (/root/printing/printlabel.sh /var/prn/.rootscript.txt.swp)
Feb  3 09:26:07 vm2263 incrond[13417]: cannot exec process: Permission denied
Feb  3 09:26:12 vm2263 incrond[27016]: (root) CMD (/root/printing/printlabel.sh /var/prn/rootscript.txt)
Feb  3 09:26:12 vm2263 incrond[13418]: cannot exec process: Permission denied
Feb  3 09:26:12 vm2263 incrond[27016]: (root) CMD (/root/printing/printlabel.sh /var/prn/.rootscript.txt.swp)
Feb  3 09:26:12 vm2263 incrond[13419]: cannot exec process: Permission denied

I then tried it as a different user, which is allowed shell access.

I set up, as the user:

home/user/printing/jobs
home/user/printing/script/printjob.sh
home/user/printing/printed

The printjob.sh contains:

#!/bin/bash
lp -d cups_printer $*
mv $* /home/user/printing/printed

I tested the user has permission to use the cups_printer cue and yes, it does print.

Then as the user, I did incrontab -e and wrote:

/home/user/printing/jobs IN_CLOSE_WRITE /home/user/printing/script/printjob.sh $@/$#

Then incrontab -d to refresh the user's tables

vim /home/user/printing/jobs/jtest.txt and add some test to be printed, :wq to save and exit

su - to root, I can then see in /var/log/cron (user can't read this log file)

Feb  3 09:43:00 vm2263 incrond[27016]: (user) CMD (/home/user/printing/script/printjob.sh /home/user/printing/jobs/.jtest.txt.swx)
Feb  3 09:43:00 vm2263 incrond[13442]: cannot exec process: Permission denied
Feb  3 09:43:00 vm2263 incrond[27016]: (user) CMD (/home/user/printing/script/printjob.sh /home/user/printing/jobs/.jtest.txt.swp)
Feb  3 09:43:00 vm2263 incrond[13443]: cannot exec process: Permission denied
Feb  3 09:43:07 vm2263 incrond[27016]: (user) CMD (/home/user/printing/script/printjob.sh /home/user/printing/jobs/.jtest.txt.swp)
Feb  3 09:43:07 vm2263 incrond[13444]: cannot exec process: Permission denied
Feb  3 09:43:51 vm2263 incrond[27016]: (user) CMD (/home/user/printing/script/printjob.sh /home/user/printing/jobs/.jtest.txt.swx)
Feb  3 09:43:51 vm2263 incrond[13449]: cannot exec process: Permission denied
Feb  3 09:43:51 vm2263 incrond[27016]: (user) CMD (/home/user/printing/script/printjob.sh /home/user/printing/jobs/.jtest.txt.swp)
Feb  3 09:43:51 vm2263 incrond[13450]: cannot exec process: Permission denied
Feb  3 09:43:55 vm2263 incrond[27016]: (user) CMD (/home/user/printing/script/printjob.sh /home/user/printing/jobs/jtest.txt)
Feb  3 09:43:55 vm2263 incrond[13451]: cannot exec process: Permission denied
Feb  3 09:43:55 vm2263 incrond[27016]: (user) CMD (/home/user/printing/script/printjob.sh /home/user/printing/jobs/.jtest.txt.swp)
Feb  3 09:43:55 vm2263 incrond[13452]: cannot exec process: Permission denied

So it seems multiple incron commands doesn't work. I expected the bash script to work but I have a permissions problem, even as root. Any further suggestions?

It's a fairly vanilla CentOS 5.2
Avatar of T0PS3O

ASKER

That was a bit matter-of-fact. Can I add a big "thank you" for the suggestions?! Really appreciate it.
ASKER CERTIFIED SOLUTION
Avatar of Nem Schlecht
Nem Schlecht
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of T0PS3O

ASKER

That's it, didn't make it executable duhhhhhh....

500 all yours, thanks a lot!
Avatar of T0PS3O

ASKER

Can I tip another 500 points? :)
;-) Happy to help.