Pages

Tuesday, February 20, 2018

How to add entry to crontab using bash script or in a single command?

How to add entry to crontab using bash script or in a single command?

It is very simple to do, just run the following command from the user in which cron needs to be created or add the command to your bash script and run the script from the user in which cron needs to be created:
Shell command:

[shell]
(crontab -l 2>/dev/null; echo "* * * * * /path/to/script or command here") | crontab –
[/shell]

If you need to add it to the root‘s crontab, then run the command after logging in as root, or run the above command with sudo or add the command to your bash script and run the script as sudo or from root user:

[shell]
(sudo crontab -l 2>/dev/null; echo "* * * * * /path/to/script or command here") | sudo crontab –
[/shell]

Although, executing above might give an error message – “no crontab for user”. However, you can ignore the error as it creates the crontab entry anyway.

No comments:

Post a Comment