Is there a way to add lines to a user's cron via script?
I usually do it using crontab -e
, but I would like to automate this task with a shell script.
Is there a way to add lines to a user's cron via script?
I usually do it using crontab -e
, but I would like to automate this task with a shell script.
You can echo the line in to the bottom of the current users crontab like this:
If you want to edit a value in your crontab, you can do something along the lines of:
Obviously you need to be careful with your substitution to be sure it only matches the line(s) you want to change; otherwise all
foos
are changed tobars
(in this example).The advantage to this method is that you aren't replacing the entire crontab. (A metaphorical tweezer rather than a sledgehammer.)
You can use any editing command instead of sed. For example, if you wanted to use ed to touch up a line that starts out looking like this:
Say this line is among many lines or you have many different crontabs to update on different systems and you only know your line is going to be the only line with the term
flitch
in it.It might look like:
Now I must admit that nearly 100% of the time sed will do what ed will do, but it's always good to have an extra tool on the Swiss army knife. ^.^