Answer the question
In order to leave comments, you need to log in
How to pass a task to cron from a script?
Hello!
The task is this. There is a certain script.
One of his tasks is to add jobs to cron
. Before writing this line, I decided to test and found the following.
If a task is added to cron in this way, then the previous ones are overwritten, which is logical in principle, but the task is executed at the specified interval (every minute):
echo "* * * * * rm -rf /home/user/testfolder" | crontab
echo "* * * * * rm -rf /home/user/testfolder" >> /var/spool/cron/crontab/root
Answer the question
In order to leave comments, you need to log in
crontab -l > foocron
echo "* * * * * rm -rf /home/user/testfolder" >> foocron
crontab foocron
rm foocron
Have you ever wondered why the documentation says - do not edit user crontab files directly?
I'll give you a hint - if the daemon constantly monitors /etc/crontab and rereads it when it changes, then no one monitors user files. And the command to reread them is given by the same crontab -e command
. And Ivan Yakushenko has already given you the correct option.
There is also /etc/cron.d/ directory. Any file added there is treated as an additional schedule. This was done specifically for package managers, so as not to edit the only /etc/crontab when installing / adding packages ...
If you add a task in this way, and at the same time, up to this point, the task has never been added to cron , then this file will simply have this entry, but without attributes, and it will not be executed later by the scheduler. Those. file is dead!
Correctly it should be like this:
Why with sudo ?
Thus, the task is written to /var/spool/cron/crontabs/root
And if without sudo , then to /var/spool/cron/crontabs/your_user_name_in_the_system
And if it has already been added, or, manually, through the editor with the command sudo crontab -e , or , as I describe above, then after the first such addition, you can already add the task with the command correctly like this:
But in this case, be sure to reload the command line scheduler:
Otherwise, the task will not work...
Or add everything manually through the editor:
Save, exit and everything is immediately ready for use!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question