简体   繁体   中英

Schedule task from script in ubuntu

I want to make a script that schedules the writing of date and time in a txt file.

*1 * * * * date >> ultimoscript.txt

Now I need to know where this crontab is or how I can write the previous code using the script.

I tried with crontab -e but it is not possible

contrab - e *1 * * * * date >> ultimoscript.txt

I need to solve this because I can not use crontab directly it has to be through a script that the program crontab.

you can edit by run command:

crontab -e

and then put in there:

* * * * * date >> ultimoscript.txt

save the crontab and restart the cron service

crontab -l > tempfile
crontab <<EOF
`cat tempfile`
* * * * * date >> ultimoscript.txt
EOF

The above code is useful if you would like to append a crontab entry to the current crontabs. Here is the explanation:

crontab -l lists the current crontab entries for the current user. Save this to a tempfile. We're going to append a new crontab entry to tempfile.

The command is just crontab. When crontab is invoked without parameters, it replaces all cron entries with its stdin. In this case, we are redirecting stdin from a heredoc (the content between <

I would consider it more readable if I wrote the code this way, but I was just giving you a quick answer above:

crontab -l > tempfile
echo '* * * * * date >> ultimoscript.txt' >>tempfile
crontab <tempfile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM