LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-27-2023, 11:22 PM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
Unhappy check if the process is running and report failure if it is not.


I have a python script running like the one below; it runs with albert user.

Code:
ps -fu albert|grep "server.py"
albert     24744   24615  0 11:34 ?        00:00:01 python server.py 8443
I have created a small snippet to check if the process is running and report failure if it is not.


Code:
cat check_process.sh
#!/bin/bash
set -x
APP_RUNNING=$(ps -fu albert|grep "server.py")

if [ -z "${APP_RUNNING}" ] ; then
    echo "App is not running, reporting exit 1"
    exit 1
else
  echo "App is running."
fi
./check_process.sh
++ ps -fu albert
++ grep server.py
+ APP_RUNNING='albert     24744   24615  0 11:34 ?        00:00:02 python server.py 8443'
+ '[' -z 'albert     24744   24615  0 11:34 ?        00:00:02 python server.py 8443' ']'
+ echo 'App is running
App is running
I am not able to figure out if
1. This is the best way to check if the process is running.

I see a lot on the internet people checking processes with pgrep, but it does not work for me.

I am not really much in shell scripting stuff but if there is any more brilliant/nicer/clean and tidy way that I can incorporate, I am super keen to hear from you.

Any guidance will be highly appreciated.

Last edited by sysmicuser; 03-28-2023 at 12:23 AM. Reason: Added some more information.
 
Old 03-28-2023, 12:27 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I'd be interested to know why pgrep 'doesn't work' for you.
It was created relatively recently (in Unix history) because so many people were writing potentially complex piped statements like yours .
For instance, you should also be adding ' |grep -v grep' to avoid a false match (think about it...).
 
Old 03-28-2023, 01:05 AM   #3
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
@chrism01
I will never require grep -v grep when I run ps -fu command? Maybe I would or rather I should if I was running "ps -ef". I am just telling based on my experience but kindly correct me if I am wrong.

When I do pgrep I don't get anything. Please check below

Code:
pgrep server.py
echo $?
1
Kindly assist.
 
Old 03-28-2023, 01:22 AM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
The return code of 1 indicates that one or more processes matched the criteria. In other words, the program is running.
See man pgrep
 
Old 03-28-2023, 02:31 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,962

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
Quote:
Originally Posted by scasey View Post
The return code of 1 indicates that one or more processes matched the criteria. In other words, the program is running.
See man pgrep
actually no, 1 means no process found.
Code:
EXIT STATUS
       0      One or more processes matched the criteria.
       1      No processes matched.
       2      Syntax error in the command line.
       3      Fatal error: out of memory etc.
but pgrep without -f will check the process name, which is python, so you ought to use pgrep -f server.py.
Even better would be to use a pid file.

Last edited by pan64; 03-28-2023 at 02:39 AM.
 
1 members found this post helpful.
Old 03-28-2023, 04:04 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,808

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Your sample
Code:
ps -fu albert | grep server.py
filters for user albert, so only if you run this as user albert you need a | grep -vw grep

Yes, pgrep is better.
pgrep by default takes the short ps name, in the case of "python server.py" it might be just "python" that is of course too unprecise.
pgrep -f
takes the full ps name.
pgrep -u albert
filters for user albert
Code:
pgrep -fu albert "server.py"
works like your sample but prints the pid(s), and can safely be run as user albert.

Consult the man page for further options
Code:
man pgrep
History:

pgrep and pkill were developed by Sun Microsystems for their Solaris, and re-engineered in Linux and some Unixes.
It takes many options that are known from ps and grep, so it is easy to use.

Another Linux command is pidof. pidof comes with some magic that might detect script names as well. But magic is hard to guess - I often need extensive test so generally prefer pgrep.

Last edited by MadeInGermany; 03-28-2023 at 04:46 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Report trigger for Ubuntu Report restarts and ends with crash ethereal1m Linux - Software 3 03-26-2021 12:09 AM
API for current running process details like process name, process id, amount of memory used anki123 Linux - General 1 01-20-2019 02:06 AM
[SOLVED] Failure after failure after failure.....etc 69Rixter Linux - Laptop and Netbook 5 04-14-2015 09:58 AM
Script to check PID from file and check whether process is running or not rajkiran183 Linux - Newbie 5 10-19-2012 11:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration