Restart Apache if failed

This bash script check web site every 5 minutes, restart the web server if web site is not responding. Create a file mkdir /usr/serverok vi /usr/serverok/check_httpd.sh Add #!/bin/bash # Author: Yujin Boby # Web: https://serverok.in # Email: admin@serverok.in /usr/bin/wget –tries=1 –timeout=30 -O /dev/null https://YOUR_DOMAIN.EXTN/ if [ $? -ne 0 ]; then systemctl restart httpd datetime=`date … Read more

Bash check if a program is running or not

You can find process id for a running program with command pidof BINARY_NAME_HERE Example boby@sok-01:~$ pidof gedit 22057 boby@sok-01:~$ ps aux | grep gedit boby 22057 4.4 0.7 822760 60280 ? Sl 14:29 0:00 /usr/bin/gedit –gapplication-service boby 22071 0.0 0.0 8904 644 pts/0 S+ 14:29 0:00 grep –color=auto gedit boby@sok-01:~$ We can use pidof command … Read more

bash check if a folder is empty or not

To check if a folder is empty of not, you can use if [ “$(ls -A /home/boby/1/ )” ] then echo “Files found” else echo ‘No files found’ fi If you want to reverse the checking, you can use ! if [ ! “$(ls -A /home/boby/1/ )” ] then echo “No files found” else echo … Read more

Bash check if a directory exists

To check if a directory exists in bash, use if [[ -d “/path/to/directory/” ]] then echo “Directory found.” fi To check for absense of a directory, use ! if [[ ! -d “/path/to/directory/” ]] then echo “Directory not found.” fi See bash

Bash if statement

Check if first augment to script is –help if [ “$1” = “–help” ]; then echo “$0 –help = shows help” exit 0 fi Check if a file exists and is executable if [ -f “/usr/bin/curl” ]; then echo “curl exists” fi Check if a binary file exists and is executable if [ -x “/usr/bin/curl” … Read more

bash

Bash shortcuts CTRL + U = Delete everything CTRL + W = Delete a word CTRL + A = Go to Line starting CTRL + E = Go to Line end CTRL + LEFT/RIGHT ARROW = move cursor by one word Enable color prompt Edit .bashrc, add following export PS1=’${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ‘ Logging Linux Commands for … Read more