How To Execute Shell Scripts
You might have seen admins executing scripts in different ways. Let's find out different ways in which we can execute Linux shell scripts.
Run Scripts in Foreground
Foreground means you need to wait until script finishes its job before you are returned to the Linux prompt. Also the job will be killed if you close terminal or loose connection to the server.
You can use below simple command to execute the scripts in foreground
sh test-script.sh
You can also give execute permissions to script and run using below
chmod +x test-script.sh --> give execute permission
./test-script.sh --> if in script folder
/root/scripts/test-script.sh --> if not in script folder
If you are running script from the scripts folder then you must use "./" in front of the script name. Else, just give complete path of the script
Run Scripts in Background
By just putting & in front of the script will make it run in background. But you cannot disconnect session from the server, else the script will fail
./test-script.sh & --> immediately returned to Linux prompt
Linux provides you with nohup utility which allows you to run scripts in background. Even if your connection to the server is lost, the script continues to run on the server
nohup script.sh &
Let's take you would like to run a script in background, capture the script log (to see later what script has done) and disconnect from the server
nohup script.sh &> output.log &
The above command will run script.sh file in background while printing what it is doing into output.log file. With the last & we are submitting the job to run in background and we can happily disconnect session from the server!
Killing Background Scripts
Whenever you submit scripts to nohup utility, Linux will prompt you with a [job id] and process id. From below image, our job id is [1] and process id is 169343
Always make a note of process id. This will allow you to kill the background job from another session!
If you are in the same putty session where you submitted nohup job, then first check all the running jobs
To kill a specific job, you can use
kill %1 --> kill job with job id
kill -9 169343 --> kill job with process id
If your session is disconnected and you are trying to kill job from another session then
ps -ef | grep 8-loop-1.sh --> search with script name
kill -9 169343 --> kill job with process id
Run Scripts from Any Location
Noticed how we can run OS level commands from any location inside server. It would be a good idea if we could also run our shell scripts from any location on server. Let's look at PATH environment variable for now
Whenever you type any command (ls) Linux will search all of the above locations to run the command. The ls command is located under /usr/bin location
If we keep our shell scripts in any of the locations mentioned under $PATH variable, we will be able to execute scripts from any location on the server (same user). One such location is /home/<user>/bin, we can create a bin folder inside user home location and place all our shell scripts
We moved all our shell scripts to /home/arun/bin folder
And now we can run scripts from any location 😜
Enjoy!