0

This might sound like a duplicate, but hear me out first.

I run a shell script, which has a standard input active and sometimes on startup requires some input. I need for it to run 24/7 and it needs restart on failure by either inputting a command or just inputting "y" if the prompt pops up. I've already tried two methods: using nohup and clicking Ctrl+Z, typing bg both show the job as stopped and it's not running. Only when I execute fg it goes active. Any clue how to solve this?

P.S I've also tried scriptcommand & it ends up with "Stopped" as well.

Would setting it up as a systemd service make sense?

2 Answers 2

1

Programs which rely on input as you described need to be started inside a terminal multiplexer like tmux (https://en.wikipedia.org/wiki/Tmux) or screen (https://www.gnu.org/software/screen/). Think of it as a text-based version of a terminal window.

Use screen my-program to start my-program. Then you can detach from your virtual screen console using the key-stroke combination Ctrl-A Ctrl-D, and you're back to the shell where you started, but your program keeps running in the background. Then, using screen -r you can reconnect to the virtual console.

screen (and your program) will keep running until you program exits or the server is rebooted.

tmux is quite similar, but has more options and features, and uses different keyboard shortcuts.

0

For the script to continue running in the background, use nohup: nohup scriptcommand &. Ideally, the script would have option to not prompt for input. If that isn't an option for some reason, Expect can be used to input 'y'.

Creating a systemd service for the script would allow for the script to be started on system reboot. You can also use Restart=always in your systemd file to restart the service if the service is exited or killed, the systemd documentation has more info.

5
  • The nohup method fails. Commented Apr 14, 2020 at 19:12
  • Do you have an error message? Or can you provide a better description?
    – nickdew
    Commented Apr 14, 2020 at 19:27
  • I get the same Stopped message. I also already wrote in the question that I've tried it. Commented Apr 15, 2020 at 15:29
  • Saying you got a "Stopped" message doesn't provide enough info to debug the problem. Check nohup.out to debug further. Best guess, the something is being run which requires interaction, which is where nohup would usually have a problem.
    – nickdew
    Commented Apr 15, 2020 at 17:07
  • That seems to be the case. Commented Apr 16, 2020 at 16:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .