- Unix / Linux — What is Shells?
- Shell Prompt
- Shell Types
- Shell Scripts
- Example Script
- Shell Comments
- Extended Shell Scripts
- How do I find out what shell I am using on Linux/Unix?
- How can I find out what shell I am using?
- How do I check which shell am I using?
- How do I check how many shells are installed on my Linux box?
- Okay, so when I open the Terminal app, which shell is opened by default?
- How to check which shell am I using:
- Conclusion
- Bash check if process is running or not on Linux / Unix
- Bash check if process is running or not
- What is a Linux or Unix process?
- Is nginx process is running or not?
- Bash check process running with pidof command
- Bash shell check if a process is running or not with ps
- Determine whether a process is running or not using a shell script
- Linux/Unix bash command to determine if process is running
- Bash shell script to check running process
- A note about service and systemctl command
- Conclusion
Unix / Linux — What is Shells?
A Shell provides you with an interface to the Unix system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program’s output.
Shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions.
Shell Prompt
The prompt, $, which is called the command prompt, is issued by the shell. While the prompt is displayed, you can type a command.
Shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.
Following is a simple example of the date command, which displays the current date and time −
You can customize your command prompt using the environment variable PS1 explained in the Environment tutorial.
Shell Types
In Unix, there are two major types of shells −
Bourne shell − If you are using a Bourne-type shell, the $ character is the default prompt.
C shell − If you are using a C-type shell, the % character is the default prompt.
The Bourne Shell has the following subcategories −
- Bourne shell (sh)
- Korn shell (ksh)
- Bourne Again shell (bash)
- POSIX shell (sh)
The different C-type shells follow −
- C shell (csh)
- TENEX/TOPS C shell (tcsh)
The original Unix shell was written in the mid-1970s by Stephen R. Bourne while he was at the AT&T Bell Labs in New Jersey.
Bourne shell was the first shell to appear on Unix systems, thus it is referred to as «the shell».
Bourne shell is usually installed as /bin/sh on most versions of Unix. For this reason, it is the shell of choice for writing scripts that can be used on different versions of Unix.
In this chapter, we are going to cover most of the Shell concepts that are based on the Borne Shell.
Shell Scripts
The basic concept of a shell script is a list of commands, which are listed in the order of execution. A good shell script will have comments, preceded by # sign, describing the steps.
There are conditional tests, such as value A is greater than value B, loops allowing us to go through massive amounts of data, files to read and store data, and variables to read and store data, and the script may include functions.
We are going to write many scripts in the next sections. It would be a simple text file in which we would put all our commands and several other required constructs that tell the shell environment what to do and when to do it.
Shell scripts and functions are both interpreted. This means they are not compiled.
Example Script
Assume we create a test.sh script. Note all the scripts would have the .sh extension. Before you add anything else to your script, you need to alert the system that a shell script is being started. This is done using the shebang construct. For example −
This tells the system that the commands that follow are to be executed by the Bourne shell. It’s called a shebang because the # symbol is called a hash, and the ! symbol is called a bang.
To create a script containing these commands, you put the shebang line first and then add the commands −
Shell Comments
You can put your comments in your script as follows −
Save the above content and make the script executable −
The shell script is now ready to be executed −
Upon execution, you will receive the following result −
Note − To execute a program available in the current directory, use ./program_name
Extended Shell Scripts
Shell scripts have several required constructs that tell the shell environment what to do and when to do it. Of course, most scripts are more complex than the above one.
The shell is, after all, a real programming language, complete with variables, control structures, and so forth. No matter how complicated a script gets, it is still just a list of commands executed sequentially.
The following script uses the read command which takes the input from the keyboard and assigns it as the value of the variable PERSON and finally prints it on STDOUT.
Источник
How do I find out what shell I am using on Linux/Unix?
B oth Linux and Unix provides various shell out of the box. One can find bash (Bourne Again shell), ksh (Korn shell), csh (C shell)/tcsh (TC shell), sh (Bourne shell) and more installed by default. However, how do you check which shell am I using? What is the best way to find out what shell I am using on Linux? The echo $SHELL is not so reliable. This page explains how to find out which shell I am using at a Linux, MacOS, FreeBSD, or Unix-like systems.
How can I find out what shell I am using?
The following echo command or printf command should work:
echo «$SHELL»
OR
printf «My current shell — %s\n» «$SHELL»
Please note that $SHELL is the shell for the current user but not necessarily the shell that is running at the moment. Try the following examples
How do I check which shell am I using?
Here is another old good Unix trick. Use the ps command with -p
ps -p $$
Sample outputs:
So what is a $ argument passed to the -p option? Remember $ returns the PID (process identification number) of the current process, and the current process is your shell. So running a ps on that number displays a process status listing of your shell. In that listing, you will find the name of your shell (look for CMD column).
ps -p $$
Sample outputs:
From my Linux box:
ps -p $$
Sample outputs:
You can store your shell name in a variable as follows :
MYSHELL=`ps -hp $$|awk ‘
Please note those are backquotes, not apostrophes. Or better try out the following if you have a bash shell:
MYSHELL=$(ps -hp $$|awk ‘
Another option is as follows:
echo $0
OR
printf «%s\n» $0
Sample outputs from the above commands:
Fig.01: Linux check which shell am I using
How do I check how many shells are installed on my Linux box?
The /etc/shells is a text file which contains the full pathnames of valid login shells. Type the following [nixmd name=”cat”] to see list how many shells are installed on your Linux or Unix box:
cat /etc/shells
Use /etc/shells file to check how many shells are installed on your system
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Okay, so when I open the Terminal app, which shell is opened by default?
Your default shell is defined in /etc/passwd file. So try the following grep command:
How to check which shell am I using:
Use the following Linux or Unix commands:
- ps -p $$ – Display your current shell name reliably.
- echo «$SHELL» – Print the shell for the current user but not necessarily the shell that is running at the movement.
- echo $0 – Another reliable and simple method to get the current shell interpreter name on Linux or Unix-like systems.
- readlink /proc/$$/exe – Another option to get the current shell name reliably on Linux operating systems.
- cat /etc/shells – List pathnames of valid login shells currently installed
- grep «^$USER» /etc/passwd – Print the default shell name. The default shell runs when you open a terminal window.
- chsh -s /bin/ksh – Change the shell used from /bin/bash (default) to /bin/ksh for your account
Conclusion
Sometimes things are not easy as they seem, and this page is the perfect example of it. I hope you found the suggestion useful when it comes to checking your current running shell. Bash users can display shell version by typing the following command:
$ bash —version
Here is what I got from my Ubuntu Linux 20.04 LTS desktop:
Источник
Bash check if process is running or not on Linux / Unix
Bash check if process is running or not
Bash commands to check running process:
- pgrep command – Looks through the currently running bash processes on Linux and lists the process IDs (PID) on screen.
- pidof command – Find the process ID of a running program on Linux or Unix-like system
- ps command – Get information about the currently running Linux or Unix processes, including their process identification numbers (PIDs).
Let us see some examples about checking processes that running or not in Linux and Unix systems.
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
What is a Linux or Unix process?
A Linux process is nothing but an executing (i.e., running) instance of a program. For example, Apache or Nginx web server runs on Linux or Unix-like system to display web pages in the background. All running process in the background is called as Daemon. So Apache/Nginx is a class of processes that run continuously in the background, and we say nginx or httpd daemon is running on the server. However, how do you verify that Nginx or HTTPD is running? You need to use the commands.
Is nginx process is running or not?
Bash check process running with pidof command
The syntax is:
pidof program
pidof httpd
pidof mysqld
pidof nginx
Bash shell check if a process is running or not with ps
Again the syntax is:
ps -C daemon
ps -C nginx
ps -C httpd
It is common to use the grep command or egrep command with ps as follows:
ps aux | grep nginx
ps aux | egrep -i «(nginx|httpd)»
Determine whether a process is running or not using a shell script
Each Linux or Unix bash shell command returns a status when it terminates normally or abnormally. You can use command exit status in the shell script to display an error message or take some sort of action. You can use special shell variable called $? to get the exit status of the previously executed command. To print ? variable use the echo command:
pgrep -x mysqld
echo $?
pgrep -x nginx
echo $?
pidof httpd
echo $?
ps -C httpd
echo $?
A 0 exit status means the command was successful without any errors. A non-zero (1-255 values) exit status means command was failure.
Linux/Unix bash command to determine if process is running
It is now easy to check if the process was found or not using exit status value:
Click to enlarge
Bash shell script to check running process
Bash if..else..fi statement allows to make choice based on the success or failure of a command:
A note about service and systemctl command
One can use systemctl command to control the systemd system under Linux. It can provide status of service too. For example, find out if nginx is running or out, run:
systemctl status
systemctl status sshd
systemctl status nginx
Older Linux distros and Unix like system such as FreeBSD use service command for the same purpose. The syntax is:
sudo service
sudo service nginx status
sudo service sshd status
Conclusion
You learned how to determine whether a process is running or not and use a conditional shell script to start/stop process based on that condition. See pgrep and bash man page here for more information.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Thank you for this article, it helped me a lot to check if a program that I run in rc.local (Raspberrry) was working or not and to check for how long it is running.
I have a trouble with the continuity of this program running. It starts automatically with the raspberry start on, but frequently the program stop working and close (never run more than two days).
I was thinking about to handling this error rebboting the system programatically every hour for example.
Could you help me to find out where to looking for a solution or if it is possible to check by a routine(code) if the program is running or not as a condition to reboot or not the system?
Источник