To start a script in the background on a Unix-like system (like Linux or macOS), so that it continues running even after you log out from the system, you can use a combination of screen for session management and & for background execution.
- Log in into your system
- Start a new screen session:
screen -S my_session - Run your script in the background, e.g.:
./something & or python my_script.py & - Detach from the screen session. Press Ctrl+A then D. This will detach you from the session, but the processes inside will continue running.
- Log out from your system. To reattach to the session later:
screen -r my_session - Listing all sessions:
screen -ls
Logging Output: If you want to log the output of your script, you can redirect it like this:
python your_script.py > output.log 2>&1 &
This command redirects both stdout and stderr to output.log.

No comments:
Post a Comment