Sponsored Content
Top Forums UNIX for Advanced & Expert Users How to check a single process instance is always running? Post 302901338 by Corona688 on Tuesday 13th of May 2014 11:53:34 AM
Old 05-13-2014
This is what daemons use PID files for.

When a daemon is launched, it creates a file, typically under /var/run/, containing its own process ID. When it quits, it deletes this file.

External things can check whether /var/run/mydaemon
1) exists
2) refers to a PID that already exists

If both these conditions are true, the process exists. Otherwise it should be restarted.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to check if another instance of the process is running

Hi, I am writing a shell script to invoke a C++ program. Before I start the C++ program (oi7loadbalancer), I am checking if the process is already running. I start the process only if it is not already running. I have the following check in my script. proccount=`ps -f -u $USER_NAME | grep... (8 Replies)
Discussion started by: sim
8 Replies

2. Shell Programming and Scripting

check process running

I have this script: ------------------------------------------------------- #!/bin/ksh # if ] || ] then echo "Executing main_load.sh script" /usr1/psc_load/jobs/cron/main_load.sh "ods" else echo "File not found, do nothing" fi exit 0 ... (4 Replies)
Discussion started by: rose1207
4 Replies

3. UNIX for Advanced & Expert Users

how to check how long the process has been running.

I have a requirement to check how long a process is running on unix system.If i use ps -ef i am getting the following message guest 2453638 1998920 0 16:16:05 - 0:00 dsapi_slave 9 8 0 but this is showing only time not the date.Can any one please advice me any script to find out how... (2 Replies)
Discussion started by: ukatru
2 Replies

4. Shell Programming and Scripting

Check if Process is running

Hi , I have a csh code below which check the process if it's running. Can any expert advise me on the following: 1) what does this notationmean ">!" and how is it different from the append ">" notation ? 2) how does "setenv" work in this code ? # Check whether there is a running... (3 Replies)
Discussion started by: Raynon
3 Replies

5. UNIX and Linux Applications

How to check if process is running?

Hi I would like to check if an instance of a script is already running. I've seen many different examples, but I haven't the slightest idea as to how to do this. Can you please help. Thank you. (5 Replies)
Discussion started by: ladyAnne
5 Replies

6. Programming

How do I check if a process is running in C

How to I check if a process is running in C? I'm trying to use ps aux |grep "process name" but failing in doing that. How do I do that? Thanks, (1 Reply)
Discussion started by: cprogdude
1 Replies

7. Shell Programming and Scripting

need to check if the process is running

Hi, I check if the process is running or not using the below. /usr/ucb/ps auxww | grep 109 |grep rmi | awk '{print $2}' 9718 Thus we see 9718 is the PID. It return blank if the process is not running. I need to perform some action if the process is not running and leave it if... (8 Replies)
Discussion started by: shifahim
8 Replies

8. UNIX for Dummies Questions & Answers

How a process can check if a particular process is running on different machine?

I have process1 running on one machine and generating some log file. Now another process which can be launched on any machine wants to know if process1 is running or not and also in case it is running it wants to stream the logs file generated by process1 on terminal from which process2 is... (2 Replies)
Discussion started by: saurabhnsit2001
2 Replies

9. Shell Programming and Scripting

Process Instance not running properly.

I have run 10 instances of the process eg, process name is BG nohup /WP01IRB1_irbapp/IRBWPROD/RB/bin/BG -c 1 -t 23 -a '-caTop TESTBILLCYCLE='5FEB13_81PT19NPT''>a.txt & nohup /WP01IRB1_irbapp/IRBWPROD/RB/bin/BG -c 2 -t 23 -a '-caTop TESTBILLCYCLE='5FEB13_81PT19NPT''>b.txt & nohup... (3 Replies)
Discussion started by: ankitknit
3 Replies

10. Shell Programming and Scripting

Check if process is running if not then use command

Hello, Could someone do the following bash ubuntu script for me? I have 5 screen processes of bot: SCREEN -dmS Xbot_instance_1 php core.php -i 1 SCREEN -dmS Xbot_instance_2 php core.php -i 2 SCREEN -dmS Xbot_instance_3 php core.php -i 3 SCREEN -dmS Xbot_instance_4 php core.php -i 4 ... (2 Replies)
Discussion started by: kotch
2 Replies
PIDFILE(3)						   BSD Library Functions Manual 						PIDFILE(3)

NAME
pidfile_open, pidfile_write, pidfile_close, pidfile_remove -- library for PID files handling LIBRARY
Utility functions from BSD systems (libbsd, -lbsd) SYNOPSIS
#include <bsd/libutil.h> struct pidfh * pidfile_open(const char *path, mode_t mode, pid_t *pidptr); int pidfile_write(struct pidfh *pfh); int pidfile_close(struct pidfh *pfh); int pidfile_remove(struct pidfh *pfh); DESCRIPTION
The pidfile family of functions allows daemons to handle PID files. It uses flopen(3) to lock a pidfile and detect already running daemons. The pidfile_open() function opens (or creates) a file specified by the path argument and locks it. If a file can not be locked, a PID of an already running daemon is returned in the pidptr argument (if it is not NULL). The function does not write process' PID into the file here, so it can be used before fork()ing and exit with a proper error message when needed. If the path argument is NULL, /var/run/<progname>.pid file will be used. The pidfile_write() function writes process' PID into a previously opened file. The pidfile_close() function closes a pidfile. It should be used after daemon fork()s to start a child process. The pidfile_remove() function closes and removes a pidfile. RETURN VALUES
The pidfile_open() function returns a valid pointer to a pidfh structure on success, or NULL if an error occurs. If an error occurs, errno will be set. The pidfile_write(), pidfile_close(), and pidfile_remove() functions return the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error. EXAMPLES
The following example shows in which order these functions should be used. Note that it is safe to pass NULL to pidfile_write(), pidfile_remove() and pidfile_close() functions. struct pidfh *pfh; pid_t otherpid, childpid; pfh = pidfile_open("/var/run/daemon.pid", 0600, &otherpid); if (pfh == NULL) { if (errno == EEXIST) { errx(EXIT_FAILURE, "Daemon already running, pid: %jd.", (intmax_t)otherpid); } /* If we cannot create pidfile from other reasons, only warn. */ warn("Cannot open or create pidfile"); } if (daemon(0, 0) == -1) { warn("Cannot daemonize"); pidfile_remove(pfh); exit(EXIT_FAILURE); } pidfile_write(pfh); for (;;) { /* Do work. */ childpid = fork(); switch (childpid) { case -1: syslog(LOG_ERR, "Cannot fork(): %s.", strerror(errno)); break; case 0: pidfile_close(pfh); /* Do child work. */ break; default: syslog(LOG_INFO, "Child %jd started.", (intmax_t)childpid); break; } } pidfile_remove(pfh); exit(EXIT_SUCCESS); ERRORS
The pidfile_open() function will fail if: [EEXIST] Some process already holds the lock on the given pidfile, meaning that a daemon is already running. [ENAMETOOLONG] Specified pidfile's name is too long. [EINVAL] Some process already holds the lock on the given pidfile, but PID read from there is invalid. [EAGAIN] Some process already holds the lock on the given pidfile, but the file is truncated. Most likely, the existing daemon is writing new PID into the file. The pidfile_open() function may also fail and set errno for any errors specified for the fstat(2), open(2), and read(2) calls. The pidfile_write() function will fail if: [EINVAL] Improper function use. Probably called before pidfile_open(). The pidfile_write() function may also fail and set errno for any errors specified for the fstat(2), ftruncate(2), and write(2) calls. The pidfile_close() function may fail and set errno for any errors specified for the close(2) and fstat(2) calls. The pidfile_remove() function will fail if: [EINVAL] Improper function use. Probably called not from the process which made pidfile_write(). The pidfile_remove() function may also fail and set errno for any errors specified for the close(2), fstat(2), write(2), and unlink(2) system calls and the flopen(3) library function. SEE ALSO
open(2), daemon(3), flopen(3) AUTHORS
The pidfile functionality is based on ideas from John-Mark Gurney <jmg@FreeBSD.org>. The code and manual page was written by Pawel Jakub Dawidek <pjd@FreeBSD.org>. BSD
October 20, 2008 BSD
All times are GMT -4. The time now is 08:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy