Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Write an app to start and kill another app periodically
Write an app to start and kill another app periodically [message #26388] Thu, 29 April 2010 17:33 Go to next message
jpderyck is currently offline  jpderyck
Messages: 15
Registered: October 2008
Location: Belgium
Promising Member
Hello,
Under Linux, I would like to write a small application which can check for a hung application periodically. If it is hung, it should kill the process and restart the application.

The application to be checked periodically was written in u++: it reads a data module acquisition connected by usb every 10 seconds. It runs for +/- 2 months and then it crash. I suppose the problem is with the usb driver or the usb module itself. I made the same program reading a different module connected by rs232 port with no problem.

I know that I have to use command like 'killall app'
How to start this kind of command from an u++ gui program ?

The best should be if the small checking app was a linux service. I saw a service app sample for windows, but does anybody have sample for linux ?

best regards
Jean-Paul
Re: Write an app to start and kill another app periodically [message #26389 is a reply to message #26388] Thu, 29 April 2010 17:51 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3356
Registered: August 2008
Senior Veteran
jpderyck wrote on Thu, 29 April 2010 17:33

Hello,
Under Linux, I would like to write a small application which can check for a hung application periodically. If it is hung, it should kill the process and restart the application.

The application to be checked periodically was written in u++: it reads a data module acquisition connected by usb every 10 seconds. It runs for +/- 2 months and then it crash. I suppose the problem is with the usb driver or the usb module itself. I made the same program reading a different module connected by rs232 port with no problem.

I know that I have to use command like 'killall app'
How to start this kind of command from an u++ gui program ?

The best should be if the small checking app was a linux service. I saw a service app sample for windows, but does anybody have sample for linux ?

best regards
Jean-Paul


Hello jpderyck

I am not an expert but this works.

You can use function int kill(pid_t pid, int sig);

pid is the id of the process to be killed.

There are these options from less to more aggresive:

#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
...
	pid_t pid;
...
	kill(pid, SIGTSTP);
	kill(pid, SIGTERM);
	kill(pid, SIGHUP);
	kill(pid, SIGKILL);


You can put sleep() between kill() functions just to give the program a little opportunity to stop itself.

To get the pid you can use getpid() and put this value somewhere where the killer process can take it.





Best regards
IƱaki
Re: Write an app to start and kill another app periodically [message #26391 is a reply to message #26388] Thu, 29 April 2010 19:18 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi jpderyck,

I'm not sure if you know what you really want Smile You were talking about GUI app on one side and periodical checks and service on the other. That doesn't get quite together Wink

If you want to have GUI, than what Koldo says is the good way for you to go. I just add other possibility, which doesn't require you to know process id: You can simply use the Sys function from U++ core:
Sys("killall -SIGXYZ app");
You can substitute the "SIGXYZ" with any of the signal names Koldo mentioned above, or omit it totally to use default SIGTERM.

The other option is a "service". Actually on linux it is called daemon, but basically it a same thing Wink That is just regular console application, and your system is set up to start or terminate it as necessary. This is usually done via initscripts. Some more advanced solutions (like upstart) also provide nice features like checking if the daemon runs, so it is restarted even when it terminates for some reason. Details depend on what distribution you use.

And there is also a third option. Don't write an U++ app at all. From what you said, I believe that all you need is a simple shell script
#!/bin/sh
# here should be some check to see if the app is running or if it hangs,
# that would depend on how it hangs and how you can test it ...
killall "yourapp"
/path/to/yourapp
Then you can set up cron to launch it periodically, let's say every 5 minutes:
echo $( crontab -l; echo '*\5 * * * * /path/to/the/script'
For more details about cron and how to use it see man cron and man crontab.

There is probably many more options, but I think the last one is just the one you need. Also it uses just standard tools, so it can be used on almost any system.

Best regards,
Honza

[Updated on: Thu, 29 April 2010 20:11]

Report message to a moderator

Re: Write an app to start and kill another app periodically [message #26408 is a reply to message #26391] Fri, 30 April 2010 10:32 Go to previous messageGo to next message
jpderyck is currently offline  jpderyck
Messages: 15
Registered: October 2008
Location: Belgium
Promising Member
Hi Honza and Koldo
thanks for reply

Of course I know that a gui app can not be as a service Smile
I was just asking me what sould be the best (and the easist thing to do)

the way I can detect that the application which realize data acquisition is hung is by checking if new data are added to the mysql database. A new data is written every 10 secs, if not, the program is not started or is hung.

I am not familiar with linux scripts (I am at first a win32 programmer) so I will continue to look in direction of a u++ application development (console or gui)

many thanks for help and advice

best regards from Belgium
Jean-Paul
Re: Write an app to start and kill another app periodically [message #26431 is a reply to message #26408] Sat, 01 May 2010 01:21 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Quote:

Of course I know that a gui app can not be as a service Smile

Well, to be fair, it can Smile There is nothing that would prevent you from doing that, you would just have to make sure either that the app doesn't start gui when run in daemon-mode or tell it on which display to run. Anyway, this is just off-topic note, don't take it seriously Wink

Good luck,
Honza
Re: Write an app to start and kill another app periodically [message #28567 is a reply to message #26431] Tue, 07 September 2010 15:16 Go to previous messageGo to next message
jpderyck is currently offline  jpderyck
Messages: 15
Registered: October 2008
Location: Belgium
Promising Member
Hi,
Back to this project, I would like to ask help again,

from my gui app, I can now kill another app with the killall command : Sys("killall -SIGXYZ app");

but how to restart it from the same gui app without wait for end?

ex: Sys("./app");
start the app but wait for end

best regards
Jean-Paul
Re: Write an app to start and kill another app periodically [message #28569 is a reply to message #28567] Tue, 07 September 2010 15:55 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi Jean-Paul,

Have a look at LocalProcess. It allows for fine grained control over an app, but for your needs it should be enough to use simply
LocalProcess("myapp -parameters ...").Detach();


Best regards,
Honza
Re: Write an app to start and kill another app periodically [message #28577 is a reply to message #28569] Tue, 07 September 2010 18:23 Go to previous messageGo to next message
jpderyck is currently offline  jpderyck
Messages: 15
Registered: October 2008
Location: Belgium
Promising Member
thanks for answer,

my problem is now:
when I use the LocalProcess method and then I kill the created app by the killall method, how can I know if the app is still really running since the command ps -N continue to show the app with a status <defunct>, is there another way to list process except those with the <defunct> ?

another way I checked is to use the Kill() method of the LocalProcess but it runs only once, I mean if I do a Start() method again after a Kill(), the next Kill() method will do nothing ! I suppose the LocalProcess should be destroyed and renewed but how to do that?

best regards
Jean-Paul
Re: Write an app to start and kill another app periodically [message #28579 is a reply to message #28577] Tue, 07 September 2010 22:07 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

jpderyck wrote on Tue, 07 September 2010 18:23

another way I checked is to use the Kill() method of the LocalProcess but it runs only once, I mean if I do a Start() method again after a Kill(), the next Kill() method will do nothing ! I suppose the LocalProcess should be destroyed and renewed but how to do that?


Congratulations, you just found a bug Wink I posted the fix in separate thread, so it gets some attention and gets into SVN faster.

As for the <defunct>: In other words, it is a zombie process. It is not really running, it just hangs there until it's parent (which is the controlling app in this case) is terminated. The cause of this is probably an imperfect design of the Detach() function. There is nothing bad about zombies ( sounds funny Very Happy ), but if you want to the controlling app running continuously, there would be a lot of them and it's just not clean solution.

So for now, I would recommend you to stay with Start() and Kill() (after fixing it) if it works for you. There is probably many other solutions to your task, but I would need to know much more about your project to tell you something useful.

Honza
Re: Write an app to start and kill another app periodically [message #28582 is a reply to message #28579] Tue, 07 September 2010 23:56 Go to previous messageGo to next message
andrei_natanael is currently offline  andrei_natanael
Messages: 262
Registered: January 2009
Experienced Member
Until LocalProcess get fixed you may use (Linux Only)
system("/path/to/app &");

It will run your application and not wait for it to return.
For windows use
LaunchWebBrowser("/path/to/app");
Re: Write an app to start and kill another app periodically [message #28607 is a reply to message #28579] Wed, 08 September 2010 10:36 Go to previous messageGo to next message
jpderyck is currently offline  jpderyck
Messages: 15
Registered: October 2008
Location: Belgium
Promising Member
thanks for bug fix

can you tell me if the Kill() method of the LocalProcess has the same effectiveness than the 'killall -SIGKILL' because the goal is to kill a hung application (I know the best thing should be to fix the application wich enter in hung state but for the moment I do not find the bug...)

best regards
Jean-Paul
Re: Write an app to start and kill another app periodically [message #28612 is a reply to message #28582] Wed, 08 September 2010 10:56 Go to previous messageGo to next message
jpderyck is currently offline  jpderyck
Messages: 15
Registered: October 2008
Location: Belgium
Promising Member
hello andrei_natanael

thanks for your suggestion:

system("/path/to/app &"); works fine but
Sys("/path/to/app &"); does not ?

finally the combination of

Sys("pidof app"); to know if app is running
Sys("killall -SIGKILL app"); to stop app and
system("/path/to/app &"); to restart the app

works fine without zombie process (<defunct>)
for dolik.rce:

I will check the other method with the LocalProcess:
IsRunning();
Kill();
Start();

with new release of upp

best regards
Jean-Paul
Re: Write an app to start and kill another app periodically [message #28618 is a reply to message #28607] Wed, 08 September 2010 11:25 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

jpderyck wrote on Wed, 08 September 2010 10:36

thanks for bug fix

can you tell me if the Kill() method of the LocalProcess has the same effectiveness than the 'killall -SIGKILL' because the goal is to kill a hung application (I know the best thing should be to fix the application wich enter in hung state but for the moment I do not find the bug...)

best regards
Jean-Paul


It uses SIGTERM to end the process. If I am not mistaken, if the process won't terminate the result will be the same as calling Detach(), so the LocalProcess is ready for next use. That would of course left your hanged up app running, until it exits whatever loop it stuck in and processes the SIGTERM.

Is there any way we could help you with the hanging app?

Honza
Re: Write an app to start and kill another app periodically [message #28632 is a reply to message #28618] Wed, 08 September 2010 16:26 Go to previous message
jpderyck is currently offline  jpderyck
Messages: 15
Registered: October 2008
Location: Belgium
Promising Member
Dear Honza

thanks for help,

the application that I need to monitor with a kind of watchdog communicates with a data acquisition module (LabJack U3) via usb port. It takes measures every 10 seconds and put results in a MySQL table. After running for a time between 1 month and 3 months, the application enter in hung state (the main window becomes blank) and the only thing to do is restart the program.

I do not know if the problem is in the usb reading part of the program or in the MySQL part.

In the 'main' I use two timers:
SetTimeCallback(-10000, callback(this, &MyApp::Timer1));
SetTimeCallback(-300000, callback(this, &MyApp::Timer2));

Timer1: do measure and store it to databases
Timer2: reduce size of one table to keep 7 days of data

maybe I could change this to :
mytimer.KillSet(10000, THISBACK(Timer1));
in the 'main'

and in the Timer1() function :
mytimer.Kill();
do the measures and others and at the end :
mytimer.KillSet(10000, THISBACK(Timer1));

but normally the code between stop and restart timer should not last more than 1 second and so I should never have re-entry problem but...

if you have any suggestion

best regards
Jean-Paul
Previous Topic: [BUG&FIX] LocalProcess can not be reused
Next Topic: String.Mid() and Unicode
Goto Forum:
  


Current Time: Tue Apr 16 14:19:26 CEST 2024

Total time taken to generate the page: 0.01833 seconds