Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am working on a program that can "upgrade itself" by install a new package. In order to accomplish this, I am using the at command to specify that a shell script should be run one minute in the future and the shell script, in turn, will install the package. I would really like this to execute sooner than one minute into the future but that appears to be the best that I can do based upon what I have read in the man page for at. Is there a way to do this with tighter resolution?

share|improve this question
What's wrong with "immediatly", by just running the program? Or, wrap it in a shell script that checks for a particular exit code and the program just has to call exit(code). – Keith Jun 9 '11 at 16:13
See also batch. But why wouldn't you just run update code in the same process or in a child one? – alex Jun 9 '11 at 16:42
The answers to yesterday's Lightweight utility/program to run a command after a random delay are easily modified to obtain a fixed delay with granularities of seconds or finer. – dmckee Jun 9 '11 at 17:35
@alex: I initially tried running the update script as a call through system() but the update didn't take place. I had reasoned that this was occurring because the first part of the update was to stop my daemon which would clobber the shell launched from my daemon. On second thought, this might have been due to my failure to redirect output. – Jon Trauntvein Jun 10 '11 at 20:18
1  
Ah, that might explain it. Stopping the daemon will send SIGHUP to it's child processes, hence your update script will terminate prematurely. Use nohup command to protect your script. – alex Jun 11 '11 at 6:22

3 Answers

up vote 3 down vote accepted

It would be easier to simply launch the program directly and have it run the update immediately (or it could sleep for some small delay if you really needed it to).

If this is in a script itself (eg, bash), simply call the update script directly (likely redirecting the output):

update-my-thingy < /dev/null > /dev/null 2>&1

If it's in a program, you can always use the system() call to do the same thing:

system("update-my-thingy < /dev/null > /dev/null 2>&1");
share|improve this answer
My initial approach was to invoke the update script as you describe above. I thought that didn't work because the child process was killed along with my daemon (the update script kills the daemon before upgrading). It could be that the failure happened because I failed to redirect output. – Jon Trauntvein Jun 10 '11 at 20:20
I had to combine this with nohup in order to prevent the child process from hanging up when my daemon was shut down. – Jon Trauntvein Jun 13 '11 at 15:17

Fork a script that waits for a few seconds.

{ sleep 5; update-my-program; } &

If your goal is to do the upgrade as soon as your program exits, but no sooner, arrange for your program to terminate by calling execve instead of exit. The execve system call replaces the current program by another. In C, there are several variants (execl, execp, etc.) depending on how you want to pass parameters. In shell, the corresponding built-in is exec.

share|improve this answer

This depends on what you program is doing. For daemons it is done the following way:

  • determine the age of the executable
  • if the age is less then the program start, then the executable was overwritten with a new version
  • in such case wait until there is no work to be done (or don't)
  • exec() on self to restart the program with a new binary
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.