Linux is among the most versatile working techniques on the planet. There’s little or no you may’t do with Linux… even automate duties utilizing a easy command line device.
The device in query is known as cron and it means that you can schedule jobs for the Linux working system.
Additionally: Patch now: Severe Linux safety gap uncovered
Say, for instance, you’ve got written a easy backup script to again up every part in your Paperwork folder. It is named backup.sh and appears one thing like this:
#!/bin/bash # What you wish to backup. backup_files="/house/$USER/Paperwork" # The place you wish to backup to. dest="/backup" # Create an archive filename. day=$(date +%A) hostname=$(hostname -s) archive_file="$hostname-$day.tgz" # Backup the recordsdata utilizing tar. tar czf $dest/$archive_file $backup_files
You save that script in /usr/native/bin and provides it the correct execution permissions with the command:
sudo chmod u+x /usr/native/bin/backup.sh
Now, as an alternative of operating that backup script manually on daily basis or week, you should utilize cron to make it automated. Let me present you the way.
create a cron job
If you happen to do not have already got your terminal window open, achieve this now.
The cron system has its personal build-in editor for cronjobs. To open your crontab in edit mode, subject the command:
If that is the primary time you’ve got issued the crontab -e command, you will want to pick your default editor. I might recommend going with nano, as that is the simplest Linux textual content editor to make use of.
On the backside of the file, you will create the brand new cron job entry. That is the place it will get a bit difficult. You see, the time/date you utilize is available in a really particular type. There are 5 entries for time and date, that are minutes (0-59), hours (0-23), day of the month (1-31), month (1-12), and day of week (0-6, though you should utilize Sunday, Monday, Tuesday, and so forth., and Sunday will be represented by 0, 7, or Sunday). For example you wish to run your backup each Sunday at 11 pm, the crontab time/date entry could be 0 23 * * 0. If you happen to needed that cron job to start out at 11:59 pm each Friday, the entry could be 59 23 * * 5.
The whole cron entry for a Saturday 11:59 PM run would seem like this:
59 23 * * 5 /usr/native/bin/backup.sh > /dev/null 2>&1
What’s the > /dev/null 2>&1 portion of the entry? Merely put, if there’s any output from the script, it should be suppressed; in any other case it may trigger errors. For that, we use the > to ship all output to /dev/null (which a like a system trash can) after which instructs cron the place to ship all errors with 2>&1.
Save and shut the file with Ctrl+X. As soon as you’ve got saved the crontab file, the job is prepared and shall be run on the configured time. Earlier than the primary run of the job, you would possibly wish to check the script to ensure it completes with out errors, which will be finished with the command backup.sh.
And that is what cron does for you and how one can simply use it to automate scripts you’ve got written for the Linux working system.