Check command status linux

Check command status linux

Last updated on: 2019-12-20

Authored by: Rackspace Support

When you need to troubleshoot a network service, the first step is to ensure that the service is running.

If the service has an initialization (init) script installed, you can use the service command to start, stop, and check the status of the service. This command references a service by using its init script, which is stored in the /etc/init.d directory for Debian®-based distributions and the etc/rc.d/init.d directory for Red Hat®-based distributions.

If you don’t know the name that the system uses for a service, check either of the preceding directories by using the ls command followed by the directory name. Some names vary depending on your distribution. For example, Apache® is httpd on CentOS® and apache2 on the Ubuntu® operating system.

Note: Many newer Linux® distributions use systemd instead of netstat to check services. If you’re using systemd , replace all service commands with systemct1 . For more information about systemct1 commands, see the Fedora™ SysVinit to Systemd Cheatsheet.

Check the service status

A service can have any of the following statuses:

  • start : The service has started.
  • stop : The service has stopped running.
  • restart : The service is rebooting and will start after the process is complete.

The following example shows how to check the status of httpd on CentOS by using the service command:

Start the service

If a service isn’t running, you can use the service command to start it. The following example starts the httpd service:

If the application can’t be started, the system reports the failure and usually displays a message that indicates the cause of the problem.

Use netstat to find port conflicts

In the preceding example, httpd can’t be started because something is already listening on the port. To find out what’s listening, you can run the netstat command.

Run the following command to display a list of listening programs and the ports that they’re using:

The output shows that the nc program (which appears in the Program name column) is listening on port 80 (which appears in the Local Address column). Stopping this program should enable httpd to be started.

Note: For more information about the netstat command, see Check listening ports with netstat.

Check xinetd status

If the service isn’t running, it might be because a super-server such as an Extended Internet Service Daemon ( xinetd ) is being used to launch the program when a connection is received. If this is the case, starting the service might have resolved the issue. Run the following command to verify that the issue is resolved:

Check logs

If you can’t start your service, review your logs to see if they contain information about the issue.

Читайте также:  Hp laserjet 1300 driver для windows hp

Next steps

After you’re sure that the application is running, check the server resources to verify how much your application is consuming.

Share this information:

©2020 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

Источник

How to view status of a service on Linux using systemctl

H ow do I use the systemctl command to view status of a systemd service on Linux operating systems?

We use systemctl status command under systemd to view the status of the given service on Linux operating systems.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux & systemd
Est. reading time 8 minutes

Viewing the Status of a Service

The syntax is as follows for the systemctl command systemctl status
systemctl status

How to view status of a service called nginx

Type:
$ systemctl status nginx.service
## ssh server status ##
$ systemctl status sshd.service
## Lighttpd web server status ##
$ systemctl status lighttpd.service

The dot (“ ● “) uses color on supported terminals to summarize the unit state at a glance. White color indicates an “inactive” or “deactivating” state. Red color indicates a “failed” or “error” state. Green indicates an “active”, “reloading” or “activating” state.

Understanding systemd service/unit states

The status of Linux service depends upon various states such as follows:

Service status Description
active (running) Service or daemon is running in the background. For example, sshd or nginx/apache web server and listing for incoming traffic.
active (exited) Service successfully started from the config file. Typically one time services configuration read before Service was exited. For example, AppArmor or Firewall service.
active (waiting) Our service is running but waiting for an event such as CPUS/printing event.
inactive Service is not running.
enabled Service is enabled at boot time.
disabled Service is disbled and will not be started at Linux server boot time.
static Service cannot be enabled on Linux, but mostly started by another systemd unit automatically. In other words, the unit file is not enabled and has no provisions for allowing in the [Install] unit file section.
masked Service is completely disabled and any start operation on it always fails.
alias Service name is an alias. It means service is symlink to another unit file.
linked Made available through one or more symlinks to the unit file (permanently in /etc/systemd/system/ or transiently in /run/systemd/system/), even though the unit file might reside outside of the unit file search path.

Currently following units are supported by systemd

  • service : Service unit configuration about a process controlled and supervised by systemd.
  • mount : File system mount point controlled and supervised by systemd.
  • swap : Swap file/disk configuration controlled by systemd.
  • socket : An IPC or network socket or a file system FIFO controlled and supervised by systemd, for socket-based activation.
  • target : It contains information about a target unit of systemd. It is used for grouping units and as well-known synchronization points during start-up. For example, graphical.target is used for GUI based desktop login. Similarly, multi-user.target is used by servers where users can log in using ssh/console.
  • device : A device unit as exposed in the sysfs/udev device tree. It includes networking and other devices.
  • automount : Automount file systems
  • timer : Cron like systemd unit to run commands and services at a given date/time format. For example, refresh firmware or clean session created by Python or PHP webapps.
  • path : A systemd special target unit that sets up all path units. For example, systemd can take certian action depend upon file system path. If /etc/foo/ modifed take some action.
  • slice : We use a systemd slice for isolating workloads. They define a hierarchy in which scopes and service is placed. The actual processes are contained in scopes or in services. Think it as lightweight Docker. For each slice, certain resource limits such as CPU or disk I/O limit may be set that apply to all processes.
  • scope : Scope units are not configured via unit configuration files, but are only created programmatically using the bus interfaces of systemd. They are named similar to filenames. A unit whose name ends in “.scope” refers to a scope unit. Scopes units manage a set of system processes. Unlike service units, scope units manage externally created processes, and do not fork off processes on its own. The main purpose of scope units is grouping worker processes of a system service for organization and for managing resources.

We can list all services unit as follows:
$ sudo systemctl —type=service
Want to see mount type units? Try:
$ sudo systemctl —type=mount
Display all systemd timer units on your Linux box:
$ sudo systemctl -t timer

To show all installed unit files use:

$ sudo systemctl list-unit-files

Linux view status of a service

Type the following command to view all services and unit status on your Linux system since boot time:
$ sudo systemctl

Click to enlarge

How to list units that systemd currently has in memory

Execute the following command:
$ sudo systemctl list-units
$ sudo systemctl list-units | more
$ sudo systemctl list-units | grep sshd
## filter by unit types ##
$ sudo systemctl list-units —type service
$ sudo systemctl list-units —type timer

List systemd/systemctl all failed units/services on Linux

$ sudo systemctl list-units —failed
$ sudo systemctl list-units —state failed
## filtering by unit type ##
$ sudo systemctl list-units —state failed —type service
$ sudo systemctl list-units —state failed —type timer

The systemctl command options to list all failed units/services

Источник

Bash get exit code of command on a Linux / Unix

I am a new Linux system user. How do I get the exit code of a command? How do I get the exit code or status of Linux or Unix shell command and store it into a shell variable?

Introduction – Each Linux or Unix shell command returns a status when it terminates normally or abnormally. For example, if backup.sh script is unsuccessful, and it returns a code which tells the shell script to send an e-mail to sysadmin.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Bash on Linux, macOS or Unix-like OS
Est. reading time 2 minutes

What is an exit code in bash shell?

Every Linux or Unix command executed by the shell script or user has an exit status. Exit status is an integer number. 0 exit status means the command was successful without any errors. A non-zero (1-255 values) exit status means command was a failure.

How to find out the exit code of a command

You need to use a particular shell variable called $? to get the exit status of the previously executed command. To print $? variable use the echo command or printf command:
date
echo $?
date-foo-bar
printf ‘%d\n’ $?

How to get the exit code of a command such as date and date-foo-bar

Bash get exit code of command – How to use exit codes in shell scripts

So how do you store exit status of the command in a shell variable? Simply assign $? to a shell variable. The syntax is:

How do I set an exit code for my own shell scripts?

The exit command cause normal shell script termination. It exits the shell with a status of N. The syntax is:

A sample shell script to get the exit code of a command

  • 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

Recommend exit code for your shell scripts

Exit Codes With Special Meanings
Exit status Description
1 Catchall for general errors
2 Misuse of shell builtins (according to Bash documentation)
126 Command invoked cannot execute
127 Command not found
128 Invalid argument to exit command
128+n Fatal error signal “n”
130 Bash script terminated by Control-C
255* Exit status out of range

How to deal with the exit codes of all piped commands

Conclusion

This page showed how to use exit codes on Linux or Unix based system and how to get the exit status/code of command. See man pages by typing the man command or help command:
man bash
help exit
For more info see:

🐧 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.

One of the best Unix exit code tutorial with examples. Thank you. I used it on my macOS macMini desktop.

Hi, I just completed 9 captchas- 18 3×3 grids. I come here for a quick tutorial to remind myself of something I already know, in most cases, for one minute so as not to lose my train of thought. Thank-you! But I don’t have the time for the captchas. Please find another solution to whatever problem you are trying to solve.

Captchas are only needed when posting comments to avoid spam. However, Cloudflare might trigger Captchas when it detects IP address with bad reputation.

Hi Vivek,
Love your site, and I’ve learned a lot from it. It’s a great resource.
I normally use a self-administered proxy/VPN when using a browser for privacy reasons. Unfortunately I can’t control which IP address I get without becoming an ISP or a cloud provider myself. Cloudflare probably sees my IP address and knows that it is in the range for cloud provider X, and not from a company or residential IP address, which triggers the captcha.
So in this case it is unfortunately targetting a normal/not-spamming user. I understand that you are doing this so as to not have to deal with spam or other issues; and my behaviour is probably that of only a tiny percentage of users.
Trade-offs and unintended consequences, right? Anyway, I thought it might be useful to hear from someone for whom this was causing issues. In order to check on this and reply today, I ‘only’ had to do 4 captchas.
Thank you kindly for your excellent website.

Источник

Читайте также:  Bootcamp удалил mac os
Оцените статью