Does crontab have an argument for creating cron jobs without using the editor (crontab -e). If so, What would be the code create a cronjob from a Bash script?
相关问题
- How to get the return code of a shell script in lu
- JQ: Select when attribute value exists in a bash a
- Invoking Mirth Connect CLI with Powershell script
- Emacs shell: save commit message
- bash print whole line after splitting line with if
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- In IntelliJ IDEA, how can I create a key binding t
- Check if directory exists on remote machine with s
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
Here is a bash function for adding a command to
crontab
without duplicationFor a nice quick and dirty creation/replacement of a crontab from with a BASH script, I used this notation:
Bash script for adding cron job without the interactive editor. Below code helps to add a cronjob using linux files.
You can probably change the default editor to ed and use a heredoc to edit.
Note the leading > in that code means the return/enter key is pressed to create a new line.
The a means APPEND so it will not overwrite anything.
The . means you're done editing.
The w means WRITE the changes.
The q means QUIT or exit ed.
you can check it out
You can delete an entry too.
That will delete the crontab entry with Myscript in it.
The d means delete the pattern inside the / /.
No check it again
This solution works inside a script too less the > of course :-)
EDIT (fixed overwriting):
Thanks everybody for your help. Piecing together what I found here and elsewhere I came up with this:
The Code
I couldn't figure out how to eliminate the need for the two variables without repeating myself.
command
is obviously the command I want to schedule.job
takes$command
and adds the scheduling data. I needed both variables separately in the line of code that does the work.Details
<(*command*)
) to turn the output ofcrontab -l
into input for thefgrep
command.fgrep
then filters out any matches of$command
(-v
option), case-insensitive (-i
option).<(*command*)
) is used to turn the result back into input for thecat
command.cat
command also receivesecho "$job"
(self explanatory), again, through use of the redirect thingy (<(*command*)
).crontab -l
and the simpleecho "$job"
, combined, are piped ('|') over tocrontab -
to finally be written.In a nutshell:
This line of code filters out any cron jobs that match the command, then writes out the remaining cron jobs with the new one, effectively acting like an "add" or "update" function. To use this, all you have to do is swap out the values for the
command
andjob
variables.