7

I have a script called foo that runs the program a.exe and sends the timing statistics to a file, time.log

#!/bin/bash
date 1>> time.log
(time ./a.exe) 2>> time.log

This works if I run the script in the background of my terminal and keep my shell open until a.exe finishes, but if I run the script in the background and exit my terminal (a.exe takes a long time to run)

foo & 
exit

when I come back, a.exe has executed but the time statistics do not appear in my log file. Does anyone know why this is? Is there a way to get the timing statistics after I've closed the parent shell?

thanks

frankscup
  • 71
  • 1
  • 1
  • 2

5 Answers5

7
nohup foo &

When you exit your shell it sends a SIGHUP signal to all child processes, which by default kills them. If you want a process to continue executing even when the parent shell exits then you need to have it ignore the SIGHUP.

NAME

nohup -- invoke a command immune to hangups

SYNOPSIS

nohup utility [arg ...]

DESCRIPTION

The nohup utility invokes command with its arguments and at this time sets the signal SIGHUP to be ignored. If the standard output is a terminal, the standard output is appended to the file nohup.out in the current directory. If standard error is a terminal, it is directed to the same place as the standard output.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
2

Since the question is tagged as bash as well, I quote from man bash

disown [-ar] [-h] [jobspec ...]
          Without  options,  each  jobspec  is  removed  from the table of
          active jobs.  If jobspec is not present, and neither -a  nor  -r
          is  supplied, the shell's notion of the current job is used.  If
          the -h option is given, each jobspec is not removed from the ta‐
          ble,  but is marked so that SIGHUP is not sent to the job if the
          shell receives a SIGHUP.  If no jobspec is present, and  neither
          the  -a  nor the -r option is supplied, the current job is used.
          If no jobspec is supplied, the -a option means to remove or mark
          all  jobs;  the  -r  option without a jobspec argument restricts
          operation to running jobs.  The return value is 0 unless a  job‐
          spec does not specify a valid job.

This comes in handy when you started a job but forgot to prefix it with nohup. Just do

disown -ah
disown -a
dennycrane
  • 2,301
  • 18
  • 15
0

Try:

nohup <your_command> &
Conner
  • 30,144
  • 8
  • 52
  • 73
khachik
  • 28,112
  • 9
  • 59
  • 94
0

Don't forget to remove all references to the tty/pts, 0</dev/null removes the stdin reference.

Rahly
  • 1,462
  • 13
  • 16
0

Your a.exe gets killed when you close the parent shell.

You could type screen, run the command as usual and exit the screen. This keeps the process from getting killed when the parent shell is exits. This method is a little cumbersome but might be handy in other situations.

John gave a much better answer - use nohup.

harithski
  • 666
  • 1
  • 6
  • 18
  • Exactly. If you know in advance that you may be starting a long-running process, or if you're on an unstable connection, you should use `screen` or `tmux`. These programs give you much more flexibility in job management and window management. – Martijn Heemels May 25 '11 at 10:12