Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to kill a job started by cron

Status
Not open for further replies.

Swanky

Programmer
Jul 17, 2001
13
0
0
US
I am writing a background process in DB/C which is new to me. I can't find anything in the language that will give me it's PID like in C. I need to start and stop this process with cron. I am having trouble finding a way to do this.

I thought maybe I could set up another cron file and run it out of there rather than root and I might get a user ID and then do a "ps -u "myuser" -o PID >> PID" and I'd get the PIDs and kill them. No luck. The user is still "?" as in any cron job.

I thought I had a work around by deleteing the processes work files in cron and that would cause an error which would terminate the program. But I realized it would kill the program at some random time in the morning when the first activity caused the error and I would have no way to know when to restart it.

Can someone suggest a way to do this? Is there a way to use "ps" to get the PID I am missing? Is there a way to store the data from "ps -ef | grep "myprogram" so that I can draw the PIDs out of it? (My program runs as parent and child.) Is there a way to force a user name onto the cron job for easy identification?
 
I have a book here that describes how to get your PID using awk. If you have used awk you will probably know the commands to find your process that is running. There is also a way in awk to stop the process. I have never used awk myself but it really doesn't look that difficult.

I hope this helps. ::)
 
I thinks this is what you want!

ps -eaf | grep myprog | awk '{printf"kill -9 %s\n",$2}' > /tmp/killprog

where myprog is the name of the program you want to stop:

You just need to run killprog...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top