Cronjob不重定向输出 [英] Cronjob not redirecting output
问题描述
我创建了一个test.sh shell脚本,已使用crontab -e安排了该脚本,每1分钟执行一次,并将输出重定向到文件。
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"
下面是我的crontab的样子
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
当我运行ls -ltr时间戳正在更新,但是输出文件中没有任何内容。
When I run ls -ltr the timestamp is getting updated but nothing is getting printed in the output file.
推荐答案
要运行 cron
每分钟,并将当前使用的环境保存到文件中,可以这样使用:
To run the cron
every minute and to save to a file the current environment used this could be used:
* * * * * env > ~/cronenv
接下来,您可以启动一个shell,就像它将在 cron
通过执行以下操作:
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"
您可以省略 sudo su
并仅使用 su
脚本运行后,您可以使用以下命令更新 cron
:
Once your script is working you could then update your cron
with:
* * * * * su - admiir -c "/path/to/test.sh > /path/to/out.txt"
您也可以将 cron
运行为特定用户的操作如下:
You could also run the cron
as the specific user by doing:
sudo crontab -u username -e
这篇关于Cronjob不重定向输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!