Most often the reasons that a beginner admin still doesn’t use scripts is because he is unable to understand what is the true advantage of writing bash scripts. Mainly profit is based of course on time and work saving. Computers can do those actions which is normaly done by humans. Of course humans make mistakes, programs don’t. But you should remember that programs are written by humans…
Writing bash scripts, no only on Linux-based system, but also on Windows takes away very much boring work. When we have to do some actions (ex: a backup each evening) these actions will be exciting only the first time. Another, and another time, this work will become more and more . Each time the next the backup will be worse because will be more boring.
Doing boring work is very dangeours, because the risk of making mistakes is rising. We can forget do backup from yesterday for example or from the month before. If you are working for yourself this problem isn’t so important. But if you are working in big company, and after breakdown you must restore backup database from 5 October 2010 (for example) which you don’t have, what do you do then?
Is this working?
First, simple and practical example us that our script will be check that another program is running. Sounds quite simple, right? The example is very usefull, because many programs must work all the time, so the situation when program crashed or simply turned off should never happen.
IRC Server (a ircd daemon), a jabber daemon, a HTTP/HTTPS daemon, a sshd daemon – this example programs must work in all time. A programmers write this programs to work constantly.
For example the OpenSSH is designed for remote management system, so if it doesn’t work, you won’t able connect to your server and manage it. Many servers are in distant location from you, other cites, countries, even continents. This could be a serious problem, when you can’t control your server because the OpenSSH isn’t work. Of course the OpenSSH is very stable program ad shoudn’t cause a spontaneus breakdown, but what are you going to do when it happens?
The simple answer for this problem is a bash script. The script will do a same action as human do (admin), but good script doesn’t require a salary and never gets tired. ;-)
Writing a scripts should be based on simply rule: You are doing some tasks manually at first, and later, if your tasks are working fine, write this tasks in a script. The script will auto repeat you tested actions saving your time in the future.
Take a look this:
localhost$ ps aux | grep /usr/sbin/sshd root 798 0.0 0.0 39588 908 ? Ss Mar31 0:00 /usr/sbin/sshd mateusz 22340 0.0 0.0 8768 1040 pts/22 S+ 18:04 0:00 grep /usr/sbin/sshd
As you can see, this command show the running processes. The /usr/sbin/sshd program is the OpenSSH exactly. We also use the grep command that shows us only the processes which we want to see. I’m going to write the script that use of the grep command to shows or doesn’t show some usefull informations. In this case, if the grep command doesn’t show any informations, it’s indicates that the OpenSSH probably doesn’t work. It’s quite simple, isn’t?
Now, we will check what is happen, when we shutdown the OpenSSH and try this command again:
localhost$ ps aux | grep /usr/sbin/sshd mateusz 22340 0.0 0.0 8768 1040 pts/22 S+ 18:04 0:00 grep /usr/sbin/sshd
As you can see the OpenSSH process doesn’t exists on the list, but some informations are still present. This isn’t important for us, so i try to hide additional grep informations by ‘-v’ the grep option.
localhost$ ps aux | grep -v grep | grep /usr/sbin/sshd localhost$
So, we have perfect situation now – because the OpenSSH program doesn’t work, our command display completely nothing. We can write the bash script which is based on this simply indicate.
#!/bin/bash ### This should be a variable, because on many systems, the OpenSSH ### can be located in different locations. SSHD="/usr/bin/sshd" ### The var contains the results of our check command IS_OPENSSH_WORKS=`ps aux | grep -v grep | grep $SSHD` ### We are checking here that is $IS_OPENSSH_WORKS empty, because this ### indicate that the OpenSSH doesn't work. if [ -z "$IS_OPENSSH_WORKS" ]; then echo "It seems that the OpenSSH isn't working. Launching..." $SSHD fi
We save our script as file (for example: openssh-check.sh), and make it executable by command:
localhost$ chmod +x openssh-check.sh localhost$
Now, we can add this script to crontab.
Leave a Reply
You must be logged in to post a comment.