[SOLVED] Check for presence of a process - from within a script
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.)
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.