Shell Script to reboot multiple servers.

Here is simple bash script where you can add the server names in the command line and then it will go on rebooting every server which you have listed.

Script Usage:-

[[email protected] /]# ./server-reboot.sh appserver1 sqlserver1 dataserver1

Above command will reboot appserver1 sqlserver1 and dataserver1, if you have not setup ssh keys it will ask for password.

To use this script create a file called server-reboot.sh and following contents.

#!/bin/bash
for server in "${@}"
do
{
scriptName=$0
scriptPath=$(dirname $0)
script=$scriptPath/boot.txt
command=`base64 -w0 $script`
ssh -t $server "echo $command | base64 -d | sudo bash"
}
done

Create a another file named boot.txt in the same location where you have created server-reboot.sh.

for ((i=03; i>0; i--)); do sleep 1 ; printf "\rREBOOTING SERVER in $i Second. PRESS CTRL + C to cancel " ; done
shutdown -rf now

Save both the files, give execute permissions to server-reboot.sh and start rebooting.

It’s just not reboot script but you can add any commands which you want to run on multiple Linux servers in the file boot.txt

There are many other ways to do the same task but this works best for my environment. Add in comments what works best for you.

Cut Command examples in Linux.

What is “cut” command in Linux?

“Cut” Command is a utility for cutting sections from each line of the Files. It can be used to cut parts of a line by byte position, character and delimiter. It can also be used to cut data from file formats like CSV.

Examples of Cut Command.
You can specify byte position using the option -b, it will show only that letter, I am using the letter JustGeek here.
If you want to display only first letter i.e. J

[[email protected] ]# echo "JustGeek" | cut -b 1
J

If you want to display the letter J,S,G i.e. 1,3 and 5th Letter then you can specify using “,”

[[email protected] ]# echo "JustGeek" | cut -b 1,3,5
JsG
[[email protected] ]#

Cut based on a delimiter:-
To cut using a delimiter use the -d option. This is normally used in conjunction with the -f option to specify the field that should be cut.
Create a file called names.csv with content below

Jeffery,Smith,45,London
Anthony,Evans,33,California
Charles,Jones,28,Paris

The delimiter can be set to a comma with -d ‘,’. cut can then pull out the fields you want with the -f flag. Example

[[email protected] ]# cat names.csv | cut -d ',' -f 1
Jeffery
Anthony
Charles
[[email protected] ]#

You can comma “,” separate for multiple entries. Example.

[[email protected] ]# cat names.csv | cut -d ',' -f 1,2
Jeffery,Smith
Anthony,Evans
Charles,Jones

If you want display age then you can just specify position with -f. Example

[[email protected] ]# cat names.csv | cut -d ',' -f 3
45
33
28

And if you want to Display City, then:

[[email protected] ]# cat names.csv | cut -d ',' -f 4
London
California
Paris

If you want to Change Delimeter in display then you can use the option –output-delimiter as shown in example below.

[[email protected] ]# cat names.csv | cut -d ',' -f 1,2 --output-delimiter='---'
Jeffery---Smith
Anthony---Evans
Charles---Jones
[[email protected] ]#

How to Check “cut” command version

 [[email protected] ]# cut --version
 cut (GNU coreutils) 8.22
 Copyright (C) 2013 Free Software Foundation, Inc.
 License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html.
 This is free software: you are free to change and redistribute it.
 There is NO WARRANTY, to the extent permitted by law.
 Written by David M. Ihnat, David MacKenzie, and Jim Meyering.

Difference between Softlink and Hardlink

Here we will see what’s exactly is soft-link first.

  1. Symbolic Link – Links the files and directories located across diferent file systems.
  2. A Softlink has different inode number, Original File and Soflinked file don’t have same inode number.
  3. Softlink can be created across different Filesystems.
  4. Deleting original folder will break Symlink

How to Identify Softlink
You can do ls -ltr, you will see a “l” in the start of the permissions.

[[email protected] ]# ls -ltr
lrwxrwxrwx 1 root root 24 Dec  9 08:57 symlink -> /usr/local/orginalfolder

How to create a softlink
Create a directory

[[email protected] ]# mkdir /usr/local/orginalfolder

and now you can create symlink using.

[[email protected] ]# ln -s /usr/local/orginalfolder symlink

In the output below, you will notice that Soft link and Orginal folder are different.

[[email protected] ]# stat symlink
  File: ‘symlink’ -> ‘/usr/local/orginalfolder’
  Size: 24              Blocks: 0          IO Block: 4096   symbolic link
Device: fd01h/64769d    Inode: 256014      Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 08:57:22.942621767 +0000
Modify: 2019-12-09 08:57:19.394593754 +0000
Change: 2019-12-09 08:57:19.394593754 +0000
 Birth: -

[[email protected] ]# stat /usr/local/orginalfolder
  File: ‘/usr/local/orginalfolder’
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: fd01h/64769d    Inode: 395887      Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 08:56:45.194323755 +0000
Modify: 2019-12-09 08:56:44.055314766 +0000
Change: 2019-12-09 08:56:44.055314766 +0000
 Birth: -

What’s the Hard-link.

  1. The main thing is you can’t create Harlink for Directories.
  2. Unlike Softlink, Hardlink can’t be created across different file-systems.
  3. Hardlinks share same inode number.

How to create a Hardlink

[[email protected] ]# touch originalFile.txt
[[email protected] ]# ln originalFile.txt hardLink.txt

Now you see a hard-link is created with same-content.

[[email protected] ]# cat originalFile.txt hardLink.txt
Hey Buddy!!
Hey Buddy!!

You will also notice that both have same inode number.

[[email protected] ]# stat originalFile.txt hardLink.txt
  File: ‘originalFile.txt’
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 256018      Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 09:17:11.070108603 +0000
Modify: 2019-12-09 09:07:39.234487185 +0000
Change: 2019-12-09 09:07:58.344638059 +0000
 Birth: -
 
 File: ‘hardLink.txt’
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 256018      Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 09:17:11.070108603 +0000
Modify: 2019-12-09 09:07:39.234487185 +0000
Change: 2019-12-09 09:07:58.344638059 +0000
 Birth: -

and if you remove original file it does not affect Hardlink

[[email protected] ]#  rm -fv originalFile.txt
removed ‘originalFile.txt’

[[email protected] ]#  ll
total 4
-rw-r--r-- 1 root root 12 Dec  9 09:07 hardLink.txt

[[email protected] ]#  cat hardLink.txt
Hey Buddy!!
[[email protected] ]# 

Error: /bin/bash^M: bad interpreter

How to fix below error?

[[email protected] /]# ./script test
 bash: ./script: /bin/bash^M: bad interpreter: No such file or directory

There nultiple way to fix this error, the problem is with the file format, you need to convert it to Unix format and your error should be gone.
You can fire the command below to fix.

[[email protected] /]# dos2unix filename

You can try steps below

 Open file, vi script
 then press ESC and enter :set fileformat=unix
 save file and try to run script again. 

How to redirect a domain name using .htaccess

If your domain name is changed for some reason and you want users/search engines to redirect to new Location then add the below code in .htaccess file of your website.

Example:

If you want to redirect http://olddomain.com to http://www.newdomain.com

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com%{REQUEST_URI} [R=301,L]
</IfModule>

After you add above code in .htaccess file it will as also redirect links.
Example:

http://olddomain.com/archive/2019 will redirect to http://www.newdomain.com/archive/2019