LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2005, 04:14 AM   #1
reygudu
LQ Newbie
 
Registered: Apr 2005
Distribution: Slackware, NetBSD, RedHat
Posts: 4

Rep: Reputation: 0
checking for a Process


"Hello World"

i have a small question, and i donīt know if my solution is the best one.

may be somebody can help me...

i have to contruct a shell script to verify that a process runs all the time

My idea is to do a small program, called by cron each 5 min:

pidno=$( ps ax | awk '/process-name/ { print $1 }' )
if test "$pidno" ; then
echo "Process process-name is running"
else
echo "Process process-name not running"
echo "Executing the process again"
/home/user/process-name
fi

Someone knows a better idea????



Thanks a lot....

Ruben
 
Old 11-18-2005, 05:03 AM   #2
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Rep: Reputation: 31
Yours seems to be a pretty good implementation. Just a couple additions id suggest:

enter the process-name with one char in [] to avoid getting the awk process also in the listing.. Also echos upon a succesful run probably arnt a good idea when running via crontab.. You dont want to be mailed every time its running normally, but perhaps you want a notification when it wasnt running and was restarted..

So the modified code:
Code:
pidno=$(ps ax | awk '/proc[e]ss-name/ { print $1 }' )
if test "$pidno" ; then
exit 0
else
echo "Process process-name not running"
echo "Executing the process again"
/home/user/process-name
fi
 
Old 11-20-2005, 07:22 AM   #3
reygudu
LQ Newbie
 
Registered: Apr 2005
Distribution: Slackware, NetBSD, RedHat
Posts: 4

Original Poster
Rep: Reputation: 0
Hi Artanicus,

thanks for your Suggestion,

finally i used:

Code:
while true 
do
  pidno=$( ps ax | awk '/pro[c]cess-name/ { print $1 }' )
  if ! test "$pidno" ; then
    echo "Executing the process again"
    /home/user/process-name  
  fi
  sleep 300
done
best regards...
 
Old 11-21-2005, 04:13 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you can also use:

pgrep -l process-name

saves all that ps| grep nonsense.
there is also pkill
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
Linux boot up process Vs. Windows boot up process darkskull Linux - Software 7 12-30-2006 04:21 PM
how can i get the process id on exec a process antony_csf Linux - Software 1 06-17-2004 03:06 AM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
problem with binding process and than execute another process chapa Programming 0 08-27-2003 03:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:11 PM.

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