Cronjob not redirecting output

2019-08-21 05:56发布

问题:

I have created one test.sh shell script which I have scheduled using crontab -e to execute after every 1 minutes and redirecting output to a file.

test.sh

echo "Printing all Environment Var"
env
echo "Bye Bye"

Below is how my crontab look like

#crontab 
0,1,2,3,4,5,6,7,8,9,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59 * * * * sudo su - admiir -c /u01/users/admiir/test.sh > /u01/app/iir/InformaticaIR/iirlog/crontab_launchsh.log

When I run ls -ltr the timestamp is getting updated but nothing is getting printed in the output file.

回答1:

To run the cron every minute and to save to a file the current environment used this could be used:

* * * * * env > ~/cronenv

Next, you can start a shell like it will be run within cron by doing:

env - `cat ~/cronenv` /bin/sh

Here you could try something like:

su - admiir -c "/u01/users/admiir/test.sh > /u01/app/iir/InformaticaIR/iirlog/crontab_launchsh.log"

You can omit the sudo su and only use su

Once your script is working you could then update your cron with:

* * * * * su - admiir -c "/path/to/test.sh > /path/to/out.txt"

You could also run the cron as the specific user by doing:

sudo crontab -u username -e


标签: shell unix cron