Seq Command in Linux

Seq Command in Linux: Linux seq command generates a list of numbers – you can use this in scripts and many other functions. Let’s see the examples.

Basic usage of command
Just typing seq 5 will create 5 numbers

[root@server ~]# seq 10
1
2
3
4
5
[root@server ~]#

If you type two numbers, first number will be start and other one will be end number.

[root@server ~]# seq 21 25
21
22
23
24
25
[root@server ~]#

You can add a third number in between which will work as a step.
for example, if you type seq 5 5 50 it will generate a number from 5 to 50 at the interval of 5 – see the example below

[root@server ~]# seq 5 5 50
5
10
15
20
25
30
35
40
45
50
[root@server ~]#

You can generate numbers in descending order as well by adding the middle number in -ve

[root@server ~]# seq 10 -1 1
10
9
8
7
6
5
4
3
2
1

Practical Usage of seq command
First, let’s see how to create files using the seq command. You will need to use format strings functions i.e. -f

[root@server seq]# touch $(seq -f "file-%g.txt" 1 10)

See the output of the command above.

You can use seq command in your scripting for example While Loops or For Loops.
We will see an example for loop.

[root@server seq]# for i in $(seq 1 10); do echo "Hello $i"; done
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10

Here is Man page you can use it to learn something more about it which may be covered in this page.

Case in shell scripting.

We use IF LOOP all time, but when it comes to multiple conditions IF Loop gets complicated as you have to write multiple times. CASE saves going through whole if…then…else statements.

#!/bin/bash
echo -n "Enter the Country name "
read Country

echo -n "Captial of $Country is "

case $Country in
        India )
       echo -n "Delhi"
       echo ""
                ;;

    Argentina )
     echo -n "Buenos Aires"
      echo ""
                ;;

    Brazil )
     echo -n "Brasília"
      echo ""
                ;;

    * )
     echo -n "unknown"
                ;;

esac

So, if you enter the country name which is mentioned it will give desired output and if it’s not present It will show unknown as mentioned in script.

[root@server ~]# ./case.sh
Enter the Country name India
Captial of India is Delhi

[root@server ~]# ./case.sh
Enter the Country name Argentina
Captial of Argentina is Buenos Aires

[root@server ~]# ./case.sh
Enter the Country name USA
Captial of USA is unknown
[root@server ~]#

Ansible Playbook to start services

Ansible Playbook to start services

We have already seen how to run Ansible Adhoc Command, now let’s focus on a playbook to start any service on your servers, In the example below I have started crond service but you can use it to suit your needs.

#Ansible Playbook to start service
-
  name: Start Services
  hosts: webservers

  become: true
  tasks:
    - name: Start Services
      service:
       name: crond
       state: started

If you have multiple host groups then replace hosts: webservers with below

 hosts: 
   - webserversUSA
   - webserversASIA
   - webserversEurope

You may have a situation where you want to start multiple services, In that case, you can use ansible loop the example playbook is as below.

#Ansible Playbook to start service
-
  name: Start Services
  hosts: webservers

  become: true
  tasks:
    - name: Start Services
      service:
        name: '{{ item }}'
        state: started
      loop:
        - httpd
        - crond
        - and-so-on
   

PS Command Examples

Below are PS Command Examples I will try to add.

Command to the process using highest memory

You can use the below PS command with the sort option which shows the process using highest %MEM usage at the bottom.

 [root@server ~]# ps aux --sort pmem
apache   29798  1.5  6.2 570424 31116 ?        S    05:11   3:32 /usr/sbin/httpd -DFOREGROUND
apache   29831  1.5  6.4 567676 32136 ?        S    05:12   3:29 /usr/sbin/httpd -DFOREGROUND
apache   18426  0.3  6.4 562656 32404 ?        S    08:57   0:01 /usr/sbin/httpd -DFOREGROUND
apache   18364  0.5  6.5 565692 32860 ?        S    08:56   0:01 /usr/sbin/httpd -DFOREGROUND
apache   29809  1.5  6.8 567452 34336 ?        S    05:11   3:31 /usr/sbin/httpd -DFOREGROUND
apache   18356  0.4  6.9 567512 34572 ?        S    08:56   0:01 /usr/sbin/httpd -DFOREGROUND
apache   29792  1.5  7.2 569516 36144 ?        S    05:11   3:33 /usr/sbin/httpd -DFOREGROUND
apache   29825  1.4  7.4 569532 37372 ?        S    05:11   3:27 /usr/sbin/httpd -DFOREGROUND
apache   18014  0.3  8.0 570868 40232 ?        S    08:51   0:02 /usr/sbin/httpd -DFOREGROUND
apache   29791  1.5  8.2 572836 41040 ?        S    05:11   3:36 /usr/sbin/httpd -DFOREGROUND

From the example above we can see that the PID 29791 is using the highest memory. You can note the PID of the process which is using the high memory and then you can see the details using ps.

[root@server ~]# ps ev --pid=18426
  PID TTY      STAT   TIME  MAJFL   TRS   DRS   RSS %MEM COMMAND
18426 ?        S      0:03   3847   483 570652 41760  8.3 /usr/sbin/httpd -DFOREGROUND LANG=C PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin NOTIFY_SOCKET=/run/systemd/notify
[root@server ~]#

Note: In the above output, if RSS (resident set size, in KB) increases over time (so would %MEM), it may indicate a memory leak in the application.

Disable host-key checking while using ansible adhoc command

If you want to disable host key checking for ansible adhoc command, first run the the command below.

 [root@server ~]# export ANSIBLE_HOST_KEY_CHECKING=False

Now, once you have disabled host key checking run your ansible ad-hoc command.

 [root@server ~]#  ansible all -i inventory -m shell -a 'uptime'

Run Ansible adhoc command with Sudo

[root@server ~]# ansible all -i inventory.yml -m shell -a 'service crond restart' -K --become