Appending to crontab with a shell script on Ubuntu

吃可爱长大的小学妹 提交于 2019-12-02 17:25:49

Use crontab -l > file to list current user's crontab to the file, and crontab file, to install new crontab.

You can also do it without a temporary file:

(crontab -l ; echo "0 4 * * * myscript")| crontab -
tal4444228

If your crontab is empty you should use 2>/dev/null:

(crontab -l 2>/dev/null; echo "0 4 * * * myscript")| crontab -

The user crontab file is in '/var/spool/cron/crontabs' for ubuntu.

adyliu@adyliu-pc:~$ sudo ls -lh /var/spool/cron/crontabs/adyliu
-rw------- 1 adyliu crontab 1.2K 2012-03-01 09:33 /var/spool/cron/crontabs/adyliu

'adyliu' is your login user.

You need root privilege to see this file.

Using "crontab -e" maybe is the best way to modify cron script.

In the manual:

Users are not allowed to edit the files under that directory directly to ensure that only users allowed by the system to run periodic tasks can add them, and only syntactically correct crontabs will be written there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!