Table of Contents
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?
crontab -e
Run a command on boot
Run a command on boot: Below commands create a job runs the file vlmscd on startup.
command="@reboot cd /home/vlmcsd && sudo ./vlmcsd -R30d -L 0.0.0.0:1688 -l /home/vlmcsd.log"
echo $command | crontab -
- : Create a variable $command to store the command.
- : Prints the command in the variable.
- : Adds the job into the crontab file.
Run the command crontab -l to verify it works.
# crontab -l
@reboot cd /home/vlmcsd && sudo ./vlmcsd -R30d -L 0.0.0.0:1688 -l /home/vlmcsd.log
Run a job with a schedule
Take a look about scheduling in cronjob. For example, cron expression means cron job will run every day at 9:00 AM. You can visit https://crontab.guru to creating and testing cron tab times or simply run this command man -s5 crontab
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>
For example, run the command at 4:00 AM every day.
command="@reboot cd /home/vlmcsd && sudo ./vlmcsd -R30d -L 0.0.0.0:1688 -l /home/vlmcsd.log"
echo "0 4 * * * $command" | crontab -
# crontab -l
0 4 * * * @reboot cd /home/vlmcsd && sudo ./vlmcsd -R30d -L 0.0.0.0:1688 -l /home/vlmcsd.log
5/5 - (1 vote)