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!
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!
If that doesn't work, try something like this:
# /var/prn IN_CLOSE_WRITE /home/username/script/do_w hatever $@/$#
Then in /home/username/script/do_w hatever put:
#!/bin/bash
lp -d cups_printer $*
mv $* /var/prn/printed
# /var/prn IN_CLOSE_WRITE /home/username/script/do_w
Then in /home/username/script/do_w
#!/bin/bash
lp -d cups_printer $*
mv $* /var/prn/printed
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.s wx)
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.s wp)
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.s wp)
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.s h
home/user/printing/printed
The printjob.sh contains:
#!/bin/bash
lp -d cups_printer $*
mv $* /home/user/printing/printe d
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/j test.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/scrip t/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/scrip t/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/scrip t/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/scrip t/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/scrip t/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/scrip t/printjob .sh /home/user/printing/jobs/j test.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/scrip t/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
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
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
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
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
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/
home/user/printing/printed
The printjob.sh contains:
#!/bin/bash
lp -d cups_printer $*
mv $* /home/user/printing/printe
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
Then incrontab -d to refresh the user's tables
vim /home/user/printing/jobs/j
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/scrip
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/scrip
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/scrip
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/scrip
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/scrip
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/scrip
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/scrip
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
ASKER
That was a bit matter-of-fact. Can I add a big "thank you" for the suggestions?! Really appreciate it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That's it, didn't make it executable duhhhhhh....
500 all yours, thanks a lot!
500 all yours, thanks a lot!
ASKER
Can I tip another 500 points? :)
;-) Happy to help.
/var/prn IN_CLOSE_WRITE lp -d cups_printer $@/$# && mv $@/$# /var/prn/printed;
Note "&&" instead of ";"