Reputation: 251
I need to write a cronjob in .sh
file. Is it possible to write a cronjob in .sh
file?
My cronjob is
*/1 * * * * bash /abc/def/ghi/sample.sh
This cronjob will be executed once every minute.
Upvotes: 1
Views: 234
Reputation: 66873
Update According to comments by Jonathan Leffler, the variable SHELL
may not be recognized by cron
(depending on the version) and is not POSIX mandated. This would make the method in the question a better option, on that account alone.
The cron will (attempt to) execute whatever is there. The way you have it, it will run bash
with following arguments, thus bash
will run your script. (Thanks to Jonathan Leffler in the comment.)
Or, you can tell cron to run all commands via the shell of your choice by setting the SHELL
variable
SHELL=/bin/bash
* * * * * /abc/def/ghi/sample.sh
See man 5 crontab
on a Linux system for a lot of detail and examples.
Given the questions posed in comments this seems appropriate to add.
To set up a cron job we need to make a "crontab" file. This is done by invoking
crontab -e
The option -e
is for "edit." The editor comes up and then you can enter the line from your question, then "save" and "quit". Which editor comes up depends on the settings, but if you didn't do anything it will most likely be vi
. The 'root' may need to 'allow' this capability for a user. You can view the contents of your crontab by crontab -l
.
When the time comes the command will get executed and the script "/path/sample.sh" will run. In that script your first line should be #!/bin/bash
. The line you show does not go in the script, it belongs to the crontab file. The rest of the script "sample.sh" should consist of code that bash
shell can execute.
NOTE Please see (additional) comments by Jonathan Leffler for more detail and expertise.
Upvotes: 2