3

I have a fairly simple shell script patch file (in essence, some scripting plus rpms all in a single file). It works great but I encountered something I wasn't expecting: a user accidentally closed the ssh session while in the middle of running it. The end result was that the older packages didn't get deleted.

The initial issue is now fixed, but it got me thinking -- is there an easy way to have a script continue operating even if a ssh session is terminated AND still provide status updates to the user via the ssh session? The initial reason why I didn't use nohup was because I thought it hid/suppressed output that I want visible to the user. While the script is running, I want the user to see status updates and such.

3

I would do something like this:

nohup command > command.out 2>&1 & tail -f command.out

Remember though tail will never exit unless killed, it keeps waiting forever -f.

3

As it has been already suggested, use -f flag of tail to read the nohup.out file continuously. Another possibility would be with named pipes.

Example:

# make a named pipe first
xieerqi@eagle:~$ mkfifo /tmp/mypipe
# redirect command there
xieerqi@eagle:~$ nohup bash -c "while [  1 ] ; do df > /tmp/mypipe ; sleep 3;done  " &
[1] 14425
xieerqi@eagle:~$ nohup: ignoring input and appending output to ‘nohup.out’

# Now cat the pipe

xieerqi@eagle:~$ cat /tmp/mypipe
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda1      115247656 83099216  26271092  76% /
none                   4        0         4   0% /sys/fs/cgroup
udev             2914492        4   2914488   1% /dev
tmpfs             585216     1152    584064   1% /run
none                5120        0      5120   0% /run/lock
none             2926072    98008   2828064   4% /run/shm
none              102400       76    102324   1% /run/user

Another example - dbus-monitor runs continuously and keeps outputting to the named pipe. Notice the residual output from previous command. May need to be cleared with echo "" > /tmp/mypipe

xieerqi@eagle:~$ nohup  dbus-monitor  > /tmp/mypipe  &
[1] 14695
xieerqi@eagle:~$ cat /tmp/mypipe 
nohup: ignoring input and redirecting stderr to stdout
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda1      115247656 83099512  26270796  76% /
none                   4        0         4   0% /sys/fs/cgroup
udev             2914492        4   2914488   1% /dev
tmpfs             585216     1152    584064   1% /run
none                5120        0      5120   0% /run/lock
none             2926072    98012   2828060   4% /run/shm
none              102400       76    102324   1% /run/user
signal sender=org.freedesktop.DBus -> dest=:1.115 serial=2 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameAcquired
   string ":1.115"
method call sender=:1.115 -> dest=org.freedesktop.DBus serial=3 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch
   string "eavesdrop=true,type='method_call'"
method call sender=:1.115 -> dest=org.freedesktop.DBus serial=4 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch
   string "eavesdrop=true,type='method_return'"
method call sender=:1.115 -> dest=org.freedesktop.DBus serial=5 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch
   string "eavesdrop=true,type='error'"
signal sender=org.freedesktop.DBus -> dest=(null destination) serial=99 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=NameOwnerChanged
   string ":1.116"
   string ""
   string ":1.116"
2

Start a Screen session. Run the bulk of the script inside a Screen session, which will exit when the script finishes. Optionally, after the Screen session finishes, print out a success/failure report.

If the user disconnects (voluntarily or not), the script keeps running unimpeded. As long as the user doesn't disconnect, they keep seeing the script's output, and can interact with it (e.g. to kill it with Ctrl+C. If they do disconnect, they can even reconnect by reattaching the Screen session.

1
nohup ls -l

#nohup: ignoring input and appending output to ‘nohup.out’
#total 44
#-rw-r--r-- 1 mikeserv mikeserv 2550 Nov  3 15:03 file
#-rw-r--r-- 1 mikeserv mikeserv   10 Nov 10 06:36 file1.tsv
#-rw-r--r-- 1 mikeserv mikeserv   10 Nov 10 06:36 file1.txt
#-rw-r--r-- 1 mikeserv mikeserv   10 Nov 10 06:36 file2.tsv
#-rw-r--r-- 1 mikeserv mikeserv   10 Nov 10 06:36 file2.txt
#-rw-r--r-- 1 mikeserv mikeserv    5 Nov 11 09:06 filea.xyz
#-rw-r--r-- 1 mikeserv mikeserv    5 Nov 11 09:06 fileb.xyz
 lrwxrwxrwx 1 mikeserv mikeserv    8 Nov 14 19:56 nohup.out -> /dev/tty
#drwxr-xr-x 2 mikeserv mikeserv   80 Nov 13 17:31 one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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