简体   繁体   中英

Running a script as a service on ubuntu server

I have a python script that I want to run on a remote ubuntu server, Currently I use the SSH using Putty to login to remote server and run the script. But this needs my terminal to be open and needs to have an active internet connection on my local machine until the task is finished.

What I need is to upload the script to server and run it as a service so that I do not need to maintain the active internet connection as well as keeping my terminal open on my local machine. Once the task is complete I need to login to server and take the result files.

How can I do this, server runs Ubuntu Server 16.04 amd64 and local machine runs Windows 10.

The quick and dirty approach:

[Putty to the remote host]
$ python your_script.py & disown %1
$ logout

Explanation: The ampersand ( & ) runs the script in the background as a job and control is immediately returned to the shell. Then, the disown %1 tells the shell to no longer watch or monitor the job allowing you to logout without killing your process.

A more professional approach might use nohup :

[Putty to the remote host]
$ nohup python your_script.py > /dev/null   # if output can be ignored
$ nohup python your_script.py > output.txt  # if output must be saved
$ logout

If you're talking about an actual service that does not require you to SSH in at all, then take your pick how you solve it -- you might consider writing a SystemD Control file, or installing a cronjob script. The latter might be an easier first approach for learning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM