Re: My way to check if a progam is already running



On a sunny day (Fri, 22 Jun 2007 13:50:11 +0200) it happened Michal Nazarewicz
<mina86@xxxxxxx> wrote in <87y7icryx8.fsf@xxxxxxxxxxxxxxxx>:

Jan Panteltje <pNaonStpealmtje@xxxxxxxxx> writes:

Lockfiles are bad, they show up at times as stale,
and then prevent your program to run.

/* This assumes there is a directory ~/.MyProgram/
struct passwd *userinfo;
char temp[4096];
FILE *fptr;
int a, b;

/* get home directory */
userinfo = getpwuid(getuid() );
home_dir = strsave(userinfo -> pw_dir);

/* test if already a MyProgram running */
sprintf(temp, "pidof MyProgram > %s/.MyProgram/pids", home_dir);
ftr = popen(temp, "w");
pclose(ftr);

sprintf(temp, "%s/.MyProgram/pids", home_dir);
fptr = fopen(temp, "r");
if(fptr) {
a = fscanf(fptr, "%d %d", &b, &b);
fclose(fptr);
if(a == 2) {
fprintf(stderr, "MyProgram: An other MyProgram "
"is already running, aborting.\n");
exit(1);
}
}

Come to think of it it doesn't work.

I can assure you it works perfectly, I am using it.
:-)
.



Relevant Pages