1

I've got a program that controls a long-running process which runs over ssh on a remote machine. Frequently, I'll get disconnected in the middle of the process, so I've changed the invocation from something like this:

my-long-cmd 

to

my-long-cmd; echo $? > /tmp/my.cmd.status 

This works; the process itself kicks off a number of subprocesses and the like, so even if the connection gets disrupted it continues running, and my script can periodically reconnect to the machine and check if the file exists (and thus, the machine is finished provisioning), and if so see whether it exited successfully or not.

However, it also means if I don't get disconnected, I'll always only see the exit status of the echo command over the ssh library. I tried doing something like echo $? && return $? but return only works in scripts. Is there an easy way to do this?

1
  • I suggest you look into using either tmux or screen. Commented Dec 5, 2014 at 21:27

1 Answer 1

1

Use exit followed by the value you want. Zero is considered success, non-zero failure. This is typically used within a script, and terminates the (sub)shell it is exited from.

If you want to capture the status of a command, assign $? to a variable. This allows you to save the value after displaying it.

2
  • my-long-cmd; echo $? > /tmp/my.cmd.status && exit $? logs out of the machine. Commented Dec 5, 2014 at 16:11
  • @ChrisB. Yes, it will exit the remote shell, but ssh should return with the exit value so you can test locally. Commented Dec 6, 2014 at 4:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.