This one is for when you actually set up your server. You cannot run script if you do not have a machine ready!
Ever wondered if there is any useful script around there to help you with your daily work on Linux? There is. Quite plenty. I will show you how to run a script manually and how to automate executing useful script so that it runs whenever you want it to run. Read on!
Before we run any code we have to have some. The best source of it is you. Only you know what do you actually need. What is the point of running a code that even though useful in general it is not useful to YOU?
Rule of thumb is that if you have a task that you are performing with ANY regularity then you can write a script that will do that for you. The only question is how to do it. I encourage you to grab a book about working with CLI, like this one for example – The Linux Command Line, 2nd Edition: A Complete Introduction. But for now I expect you will be grabbing script from GitHub and wondering how to make it work.
Running useful script – manually
In order to execute a script file named our_script.sh you just have to type the name prefixing it with ./ like so
But you have to be in the same directory where the script is located. If you are in a different directory, then instead of prefixing the name with ./ just use the full path to our script, like that
That is basically all, after the command the script is executed.
Running useful script – automatically
Utilizing a tool called cron we can automate execution of script. Cron comes with Linux by default so you do not have to install any additional packages it is included out-of-the-box. There are many ways to utilize the tool, but we will focus on the most basic usage – the crontab
CAUTION as the script will be executed without any user input, make sure the script stays in the same directry!
If you followed the basic security tutorial a directory for useful script is already there, otherwise create a directory where you will keep any automated script.
Now place the script that is supposed to be executed inside that folder and afterwards run the following command
This will bring up a file in which, at the very bottom, you will put instruction on when and what to execute. Scheduling tasks has the following form
* * * * * /path/to/script
Asterisks are responsible for (left to right)
- Day of month
- Day of week
You can substitute asterisk with a number to define when the task should be performed.
Leaving an asterisk ‘*’ will mean at every. Keep that in mind and take a look at a few examples
* * * * * /path/to script
Above line means every minute. Script will run every 60 seconds! Unless you have a very specific usage for running a certain script every minute this may utilize so much of you system resources that your server will grind to a halt. It will add a new task on top on an unfinished one to infinity and beyond. Or until everything breaks.
0 * * * * /path/to/script
Above line will make the script run every hour. It translates to At minute zero.
0 0 1 10 * /path/to/script
This one is for when you want to wake up Billie Joe Armstrong. It translates to Minute zero of hour zero of the first of October.
If you have any trouble setting up a timeframe for your crontab use this online tool to help you.
Few things to keep in mind when using crontab
- Do not schedule many tasks for the same time, it will unnecessarily consume resources
- Schedule tasks one after another, but make sure you know how much a certain task will take
- Keep the file organized! Put tasks in order of the most frequent at the top of your list to the least frequent at the bottom.
Test the execution of crontabbed script by setting it up 2 minutes in the future then close the file. If all goes well you will have the result and afterwards you can change the frequency.
Is there a difference between running crontab as current user and root? Yes, gigantic!
Crontab -e opens crontab for the CURRENT USER, which when you run the command with sudo is… root. Once you schedule any task as one and run the command as the other you will notice that is scheduled only in one of two crontabs.
Unless you need to perform a task that needs administrative privileges, execute everything as your current user. Overusing sudo is a potential security breach. You should read up on the topic, but for now just trust me. Do not run commands as sudo unless you absolutely need to.