I have written a program which calculates the amount of battery level available in my laptop. I have also defined a threshold value in the program. Whenever the battery level falls below threshold i would like to call another process. I have used system("./invoke.o") where invoke.o is the program that i have to run. I am running a script which runs the battery level checker program for every 5 seconds. Everything is working fine but when i close the bash shell the automatic invocation of invoke.o is not happening. How should i make the invoke.o to be invoked irrespective of whether bash is closed or not??. I am using UBUNTU LINUX
Asked
Active
Viewed 1,767 times
2
-
1Any of a large number of [other](http://stackoverflow.com/questions/3720439/) [questions](http://stackoverflow.com/questions/3407197/) on [StackOverflow](http://stackoverflow.com/questions/958249/) will provide you with answers about how to daemonize a program. The simplest is to run the program in background with `nohup`, as in `(nohup yourprogam &)`. – Jonathan Leffler Feb 06 '11 at 22:22
-
for me only `systemd-run
` is working – Viktor Mukhachev Feb 15 '23 at 10:43
3 Answers
3
Try running it as: nohup ./myscript.sh
, where the nohup
command allows you to close the shell without terminating the process.

poundifdef
- 18,726
- 23
- 95
- 134
0
You could run your script as a cron job. This lets cron set up standard input and output for you, reschedule the job, and it will send you email if it fails.
The alternative is to run a script in the background with all input and output, including standard error output, redirected.
While you could make a proper daemon out of your program that kind of effort is probably not necessary.

DigitalRoss
- 143,651
- 25
- 248
- 329
0
- man nohup
- man upstart
- man 2 setsid (more complex, leads to longer trail of breadcrumbs on daemon launching).

bmargulies
- 97,814
- 39
- 186
- 310