Home > other >  How to start a screen and run a python script by command
How to start a screen and run a python script by command

Time:11-05

I'm working on a bash script which starts all my python scripts, I tried running this commands in a bash file

cd /home/project1 && screen -dmS project1 python3 project1.py
cd /home/project2 && screen -dmS project2 python3 project2.py

But this doesn't start the screen and doesn't run the commands, how can I make the scripts start after their screens started?

CodePudding user response:

Ideally is to run those scripts using systemd as a service. First, create a file called /usr/lib/systemd/system/project1.service with the content bellow:

[Unit]
Description=Project1

[Service]
Type=simple
ExecStart=/usr/bin/python3 /path/to/project1.py

[Install]
WantedBy=multi-user.target

Save the file and run:

systemctl enable project1.service
systemctl start project1.service

The script should be running at this point, as a systemd service.

This is just a basic example. Systemd has a lot of configurations and functionalities and you can configure/adapt to suit your python script needs.

To see the status of the service you can issue this command: systemctl status project1.service

To see the service logs in real time you can do something like: journalctl -fu project1.service

  •  Tags:  
  • bash
  • Related