PDA

View Full Version : bash : $_ in cronjob is empty



ashalaenko
July 30th, 2012, 01:38 PM
cd some_dir && php some_crone.php || echo "$? $_" | xargs ./some_handler.php
So when I run this code from console I got a correct value for $? and for $_

$? - exit status $_ - Gives the last argument to the previous command. At the shell startup, it gives the absolute filename of the shell script being executed.

But when I put this code to cronjob :

crontab -e
*/1 * * * * cd some_dir && php some_crone.php || echo "$? $_" | xargs ./some_handler.php
I got an empty $_. Please help. I can not understand what a problem?

Lars Noodén
July 30th, 2012, 01:52 PM
You might want to wrap cd some_dir && php some_crone.php || echo "$? $_" | xargs ./some_handler.php inside a script and then call the script from cron. I sometimes find that is necessary with complex actions.

spjackson
July 30th, 2012, 02:22 PM
But when I put this code to cronjob :

crontab -e
*/1 * * * * cd some_dir && php some_crone.php || echo "$? $_" | xargs ./some_handler.php
I got an empty $_. Please help. I can not understand what a problem?
Change crontab's default shell from /bin/sh to /bin/bash by adding the following line to your crontab.


SHELL=/bin/bash

ashalaenko
July 30th, 2012, 03:35 PM
Thanks. Created bash script script and it works well. So about
SHELL=/bin/bash - I could not change cron variables - had not permissions