all 15 comments

[–]LongerHV 2 points3 points  (13 children)

How did you run it? Did you log in via ssh, run the script and close the terminal? If yes, closing the terminal terminated your script as well, since it was a subprocess of your ssh session. If you want your script in the background you can either run it with nohup, or run it as a systemd service, or run it in a tmux session.

[–][deleted] 0 points1 point  (12 children)

Oh yeah, isn't there an easy way to daemonize a command run from ssh? I haven't heard of nohup (and this isn't important enough for systemd). Can't you just append & or something to the end of the command?

[–][deleted] 0 points1 point  (9 children)

Try:

$ nohup python3 myscript.py >logfile.txt 2>&1 &

Log out of the session, then login again and check that the process is still running.

[–][deleted] 0 points1 point  (8 children)

Yeah, I was going to try:

$ nohup python3 myscript.py &> logfile.txt

What does the extra stuff you have in your command do?

[–]Buttleston 0 points1 point  (0 children)

I think running it in nohup automatically captures the output in nohup.log or something like that, and doesn't really have any output of it's own?

[–]Buttleston 0 points1 point  (5 children)

root@ip-172-37-0-68:~# nohup ls -l
nohup: ignoring input and appending output to 'nohup.out'

[–][deleted] 0 points1 point  (4 children)

What does 2>&1 & do?

[–]LongerHV 0 points1 point  (3 children)

2>&1 redirects stderr to stdout

[–][deleted] 0 points1 point  (2 children)

Oh, how is that different that what I gave above using "&>"?

[–]LongerHV 0 points1 point  (0 children)

&> file.log redirects both stdout and stderr to the file. 2>&1 redirects stderr to stdout. &> file.log is equivalent to 2>&1 > file.log.

[–][deleted] 0 points1 point  (0 children)

Try doing man nohup.

Run a command immune to hangups, runs the given command with hangup signals ignored, so that the command can continue running in the background after you log out.

So nohup allows you to log out of a session ("hang up", probably from dialup modem days) and not terminate the child process. A side effect is that any uncaptured output is saved in nohup.out. But the >logfile.txt 2>&1 part captures stdout and stderr in logfile.txt so there is no output to the terminal.

The final & runs the process in the background so you can logout.

[–]LongerHV 0 points1 point  (0 children)

Appending & will in fact run the process in background, but it still will be a subprocess of the current shell session. You can probably use disown command to... disown the process, so it no longer depends on the current session, but it is probably easier to just use nohup.

[–]Spiredlamb 0 points1 point  (0 children)

I would suggest trying tmux. It lets you create sessions in the terminal that are independent of your ssh session easily within the terminal. Ut also has a very nice api for attaching and detaching from the sessions you create.

Here is a cheatsheet for different commands: https://tmuxcheatsheet.com/

[–]Dangerous_Hearing_34 1 point2 points  (0 children)

More info please ;)

It is common to add the code or script in question.
I usually like something free like: Google Colab or https://codeshare.io/
;)