LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   runing commands (https://www.linuxquestions.org/questions/linux-newbie-8/runing-commands-451720/)

asheesh.tyagi 06-05-2006 04:32 AM

runing commands
 
Is there any way of executing a command automaticaly if it dies.i run a command
'mysql -u --password=xxxxxxx logs </var/log/mysql.pipe &' and it dies after 2-3 days.
can anyhow status of a command be checked and if it is not running then can it be executed
automaticaly.

prozac 06-05-2006 04:40 AM

write a script that checks for the status of the command and re-launches it if already dead and put it in crontab to run automatically as often as you like.

asheesh.tyagi 06-05-2006 10:57 AM

how can i check the status of command
'mysql -u --password=xxxxxxx logs </var/log/mysql.pipe &' in a script.

cs-cam 06-05-2006 05:39 PM

Code:

while true; do
  mysql -u --password=xxxxxxx logs </var/log/mysql.pipe
done

That will loop forever starting MySQL but without the ampersand at the end to background it there will only ever be one instance running at a time.

asheesh.tyagi 06-06-2006 03:02 AM

if i run the command
'mysql -u --password=xxxxxxx logs </var/log/mysql.pipe' (without &)while a process of this command is allready running in the background ,it start a new process of this command. output of ps is
root 16886 0.1 0.0 3668 1244 pts/27 S 13:16 0:00 /opt/mysql/bin/mysql -u root --password=x xxxxxx syslog
root 16946 0.0 0.0 5184 1240 pts/27 S 13:23 0:00 /opt/mysql/bin/mysql -u root --password=x xxxxxx syslog

so if a run this command from a loop.will it not create another instances.please help

Thanks

cs-cam 06-06-2006 03:49 AM

Kill the current instance then run the loop. How hard was that? You're without a MySQL server for all of about 3 seconds if you're quick.

ethics 06-06-2006 04:15 AM

Assuming he is sitting infront of it all day?

I wish i could get paid for restarting a process all day :(.

prozac 06-06-2006 05:26 AM

when i did this
Code:

$ ps aux  |grep /usr/local/mysql
i got this in my system
Quote:

mysql 585 0.0 1.2 49596 1616 ? S Mar31 0:14 /usr/local/mysql/
maybe you can do this with a script
Code:

ps aux |grep /usr/local/mysql
if [ "$?" == "1" ]; then
  launch mysql again;
fi

and put it in your cron to run every minute or hour as you like.

[EDIT]
it seems
Code:

$ ps aux  |grep /usr/local/mysql
returns 0 even when there is no actuall mysql process running. it shows me this ;)
Quote:

root 2829 0.0 0.3 4488 672 pts/0 S+ 16:21 0:00 grep /usr/local/mysql
maybe you can grep for process id instead. when the process starts awk its process id and save it and check for the same id next time. if its not there just loop the whole process else sleep. maybe it will work. i am not trying!
[/EDIT]

asheesh.tyagi 06-07-2006 07:21 AM

Thanks everybody
I have created a script and put this code in it
while true; do
mysql -u root --password=xxxxxxx logs </var/log/mysql.pipe
done

and ran it. it is working. now whenever this process get killed a new process get started automatically.

asheesh.tyagi 06-07-2006 07:28 AM

prozac if i run ps aux |grep /usr/bin/mysql and then echo $? it will always give 0 because command
ps aux |grep something
will allways run successfully whether it find a process or not.

so variable $? will allways contain 0 (for successfully running command).


All times are GMT -5. The time now is 06:49 PM.