我想用cron作为特定的用户来运行一个作业。 我已经能够成功地使用crontab来写入/ var / spool / cron / crontabs /用户名,如下所示:
@reboot ./run.sh >>~/tracefile 2>&1
但是,我想使用/etc/cron.d/filename。 在这个文件中的Cron作业需要额外的列来指示用户运行,所以我使用:
@reboot wwwuser ./run.sh >>~/tracefile 2>&1
这似乎并不奏效。 我应该能够在cron.d文件中使用带有用户名的@reboot吗?
我认为,而不是将@reboot wwwuser ...添加到/var/spool/cron/crontabs/username ,您应该以用户wwwuser运行crontab -e并添加:
@reboot ./run.sh >>~/tracefile 2>&1
为了以防万一,请确保使用脚本的完整path。
如果您以编程方式进行这些更改,则可以尝试以下操作:
#write out current crontab crontab -u wwwuser -l > mycron_wwwuser #echo new cron into a temp cron file echo "@reboot ./run.sh >>~/tracefile 2>&1" >> mycron_wwwuser #install new cron file crontab -u wwwuser mycron_wwwuser #remove the temp cron file rm mycron_wwwuser
…或更好的这一个:
crontab -u wwwuser -l | { cat; echo "@reboot ./run.sh >>~/tracefile 2>&1"; } | crontab -u wwwuser -