A
A
Alexander F2020-01-18 00:15:22
linux
Alexander F, 2020-01-18 00:15:22

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

If added in this way, then the task is added to the end of the file with tasks, but... it is not executed at all:
echo "* * * * * rm -rf /home/user/testfolder" >> /var/spool/cron/crontab/root

Tell me where am I doing wrong?
In the second option, after adding, I tried to restart cron, but it does not work (

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ivan Yakushenko, 2020-01-18
@lex63

crontab -l > foocron
echo "* * * * * rm -rf /home/user/testfolder" >> foocron
crontab foocron
rm foocron

This functionality was made specifically for such tasks, there is no need to pick default files with scripts, this is a bad idea.

R
Ruslan Fedoseev, 2020-01-18
@martin74ua

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 ...

V
Valdemar Smorman, 2020-01-20
@smorman

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!

A
Alexander, 2020-01-18
@shabelski89

use sed to edit.
for example, replacing the 3rd line in the crontab file:

sed -i "3c\* * * * * rm -rf /home/user/testfolder" /var/spool/cron/crontabs/root

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question