LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-10-2022, 10:22 AM   #1
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Rep: Reputation: 174Reputation: 174
Check for presence of a process - from within a script


MANY years ago I learned to check for the presence of a process by this technique
Code:
/ken@t31-magic:~$ ps aux | grep govpn.sh
ken         1540  0.0  0.0   9492  3412 ?        S    11:04   0:00 /bin/bash /home/ken/bin/govpn.sh
ken         2559  0.0  0.0   9032   712 pts/0    S+   11:07   0:00 grep --color=auto govvpn.sh
I can see that my daemon script is running as process 3492. However, if I interrogate the exit code from this command it is always 0 as the bash command was successful
Code:
ken@t31-magic:~$ ps aux | grep no_such_process
ken         3127  0.0  0.0   9036   720 pts/0    S+   11:18   0:00 grep --color=auto no_such_process
ken@t31-magic:~$ echo $?
0
I am looking for a suggestion for how to check for the presence of a process by name to use withing a script. Any suggestions?

TIA,

Ken
 
Old 07-10-2022, 10:34 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,361
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
I would look at pgrep and pkill first, to see if those meet your needs.

Code:
pgrep -d , -x bash
You can format the output to be comma delimited for input into ps if you need the advanced information that ps provides.

Code:
ps -p $(pgrep -d , -x bash) -o pid,user,args
That finds all the instances of Bash running, though the second example gives a lot more information through ps.
 
1 members found this post helpful.
Old 07-10-2022, 11:02 AM   #3
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 997

Rep: Reputation: 382Reputation: 382Reputation: 382Reputation: 382
I thought I was losing my mind when your 'failed' (2nd) example returns zero!

But the answer is hiding in plain sight! The line of output (which constitutes success!!!) contains your string! Just look! 'Duh'?

Last edited by !!!; 07-10-2022 at 11:04 AM.
 
Old 07-10-2022, 01:59 PM   #4
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Original Poster
Rep: Reputation: 174Reputation: 174
Thanks Turbocapitalist,

pgrep is perfect! I just want to be able to check to see that my VPN controlling daemon is running and take some action if it dies.

Thanks !!!,

I have lost my mind examining exit codes some times. The world can be falling apart in the program but if the LAST command is successful the exit code is 0.

Ken
 
Old 07-11-2022, 01:48 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,361
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
No problem.

If you are using only Bash, and not any other shell, and you have a long chain of pipes then you can use

Code:
set -eo pipefail
Or otherwise check the exit code for a specific part of series of pipes:

https://www.cyberciti.biz/faq/unix-l...iped-commands/
 
1 members found this post helpful.
Old 07-11-2022, 02:32 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,048

Rep: Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349
Quote:
Originally Posted by taylorkh View Post
Thanks Turbocapitalist,

pgrep is perfect! I just want to be able to check to see that my VPN controlling daemon is running and take some action if it dies.

Thanks !!!,

I have lost my mind examining exit codes some times. The world can be falling apart in the program but if the LAST command is successful the exit code is 0.

Ken
Not really, it is not about the exit code, but: you were looking for a command containing the given keyword and the result was the grep command (containing the given keyword). So your one-liner found itself instead of the daemon.
The correct way to look for a daemon is checking its own pidfile, which will contain the pid of the daemon process unambiguously.
 
1 members found this post helpful.
Old 07-11-2022, 07:21 AM   #7
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Original Poster
Rep: Reputation: 174Reputation: 174
Thanks pan64,

That is certainly a more rigerous approach. However, my "daemon" is simply a script file run by a common user using nohup. The root cause of all this mess is that, as usual, Proton is giving Linux users bottom priority in application development. Their "command line" VPN tool requires an active desktop session to run. It will only run from a terminal launched from within that session. It appears that the issue is that the tool is using the gnome keyring to store the VPN credentials. This makes it problematic to run on a headless box. I have worked around this with auto-login and a couple of other tricks. It is not the best solution security wise but...

It is running on an Intel NUC dedicated to the purpose and is only supporting a home network. Nothing corporate.

It appears that I would have to add code to my script to generate the pid file. I may try that just to learn how. For my purposes I think the pgrep approach will be adequate. Whenever I launch a browser on a machine on my home LAN the startup page is ipaddress.com. If I see my ISP's address I know that there is a VPN issue (or I have instructed the daemon to disconnect the VpN.)

Thanks again for the insightful comments.

Ken
 
Old 07-11-2022, 07:43 AM   #8
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 2,127

Original Poster
Rep: Reputation: 174Reputation: 174
Thanks again Turbocapitalist,

That is fantastic! I never cease to be amazed at the things bash can do - provided the nut behind the keyboard is sharp enough to figure it out.

I am a pick and shovel programmer. I started with Fortran IV in engineering school in 1971 - on punched cards I did some programming with HP programmable pocket calculators after that. When I purchased my Osborne suitcase computer in 1973 I managed to get a copy of dBaseII at a bargain price as Osborne had just gone bankrupt. xBase (dBase II, III+, Foxbase+, FoxPro, Visual FoxPro) was my forte. If I could get my hands on some data in electronic format I could do and did do ANYTHING with it.

"You need to add 3 profiles to 2,500 mainframe users and you only have 30 minutes in the project schedule for the task?" No problem. I pulled down the list of usrs, loaded them into a table, used SQL to write a script "tss add(userID) profile(profile1, profile2...)" I prepared the script ahead of time and passed it to the IT Security fellow who had access to make the changes. He ran it in about 30 seconds.

I did some application development in FoxPro as well. Being retired I find myself doing a lot with bash although I have VisualFoxpro running under wine as a backup

Ken
 
Old 07-11-2022, 08:05 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,048

Rep: Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349Reputation: 7349
this is about pid files (to be able to start with something): https://superuser.com/questions/2384...om-bash-script
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
question: 'onclick' within 'onmouseover' within 'form' within 'table' - how is it possible? rblampain Programming 4 04-25-2017 08:49 PM
Inquiry:How to check for the presence of internal Bluetooth device? hadimotamedi Linux - Newbie 1 09-24-2009 02:53 AM
Check presence of HDs in grub before boot? Jykke Linux - Hardware 1 04-06-2008 03:57 PM
How to make wvdial check for the presence of a dial tone BEFORE dialing? chuckleberry Linux - Software 0 12-28-2005 04:33 PM
How to check up a disk on presence of mistakes - logic and physical? ukrainet Linux - Newbie 2 11-19-2004 08:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:56 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