Skip to content Skip to sidebar Skip to footer

What Are The Alternatives To "python3 Sample_program.py &" Via Ssh?

I am running a python script sample_program.py on python via ssh. I log into the machine, and run python3 sample_program.py & and log off with the command 'exit'. Unfortunate

Solution 1:

nohup

nohup python3 sample_program.py &

is the simplest way (man nohup):

nohup - run a command immune to hangups, with output to a non-tty

and IMHO it is installed everywhere.

Solution 2:

at

You can use the at command. The at execute commands at a later time. The at utility shall read commands from standard input and group them together as an at-job, to be executed at a later time. For more information, options, examples, and others see the [Ubuntu Manpage Repository][1]

Example:

at now +8 hours -f python3 sample_program.py

You can also use convenient shorthands, like tomorrow or noon, as in

echo"tweet fore" | at teatime 

Independently of any terminal

ssh root@remoteserver'/root/backup.sh </dev/null >/var/log/root-backup.log 2>&1 &'

You need to close all file descriptors that are connected to the ssh socket, because the ssh session won't close as long as some remote process has the socket open. If you aren't interested in the script's output (presumably because the script itself takes care of writing to a log file), redirect it to /dev/null (but note that this will hide errors such as not being able to start the script).

Using nohup has no useful effect here. nohup arranges for the program it runs not to receive a HUP signal if the program's controlling terminal disappears, but here there is no terminal in the first place, so nothing is going to send a SIGHUP to the process out of the blue. Also, nohup redirects standard output and standard error (but not standard input) to a file, but only if they're connected to a terminal, which, again, they aren't.

You can set a cron job.

For example if now the time is 14:39:00 and today is friday, 30 august, you can add the following cron job (to be executed after 8 hours) in your crontab file using crontab -e command:

39 22 30 8 5  /path/to/python3 /path/to/sample_program.py

Solution 3:

Add the shebang to the start of your scripts!

 #!/usr/bin/python3

Give it permissions to execute.

chmod +x python3

Execute remotely!

sudo nohup ./python3 >/dev/null 2>&1 & 

This way it will run as a background process and detach from the terminal, and you will not be writing an unnecessary nohup.out file.

You DO NOT even need the .py file extension in Linux, nor do you need to use more characters than needed:

{ python3 python3.py } 

is just the same with

{ ./python3 } 

It just needs the shebang and to be executable.

Post a Comment for "What Are The Alternatives To "python3 Sample_program.py &" Via Ssh?"