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
You can add to the crontab as follows:
Cron line explaination
Source nixCraft.
No, there is no option in crontab to modify the cron files.
You have to: take the current cron file (crontab -l > newfile), change it and put the new file in place (crontab newfile).
If you are familiar with perl, you can use this module Config::Crontab.
LLP, Andrea
script function to add cronjobs. check duplicate entries,useable expressions * > "
tested :
source : my brain ;)
My preferred solution to this would be this:
This will make sure you are handling the blank new line at the bottom correctly. To avoid issues with crontab you should usually end the crontab file with a blank new line. And the script above makes sure it first removes any blank lines with the "grep ." part, and then add in a new blank line at the end with the "\n" in the end of the script. This will also prevent getting a blank line above your new command if your existing crontab file ends with a blank line.
There have been a lot of good answers around the use of crontab, but no mention of a simpler method, such as using
cron
.Using
cron
would take advantage of system files and directories located at/etc/crontab
,/etc/cron.daily,weekly,hourly
or/etc/cron.d/
:In this above example, we created a file in
/etc/cron.d/
, provided the environment variables for the command to execute successfully, and provided theuser
for the command, and thecommand
itself. This file should not be executable and the name should only contain alpha-numeric and hyphens (more details below).To give a thorough answer though, let's look at the differences between
crontab
vscron/crond
:For those who want to run the job in the context of their user on the system, using
crontab
may make perfect sense.For those who use configuration management or want to manage jobs for other users, in which case we should use
cron
.A quick excerpt from the manpages gives you a few examples of what to and not to do:
Managing crons in this manner is easier and more scalable from a system perspective, but will not always be the best solution.
Chances are you are automating this, and you don't want a single job added twice. In that case use:
This only works if you're using BASH. I'm not aware of the correct DASH (
sh
) syntax.Update: This doesn't work if the user doesn't have a crontab yet. A more reliable way would be:
Alternatively, if your distro supports it, you could also use a separate file:
Found those in another SO question.