Lyux Lyux - 2 months ago 8x
Bash Question

Start a Pythonscript in a Screen session

i am currently working on a little bash script to start a .py file in a Screen session and could use help.

I have these 2 Files: (located at /home/developer/Test/):

import os

os.system("ping -c 5>>/home/developer/Test/test.log") (located at /home/developer/):



if ! screen -list | grep -q "foo";
Status="not running"

echo "Status: $Status"

read -p "Press [Enter] key to start/stop."

if [[ $Status == "running" ]]
screen -S foo -p 0 -X quit
echo "Stopped Executing"
elif [[ $Staus == "not running" ]]
screen -dmS foo sh
screen -S foo -X python /home/developer/Test/
echo "Created new Instance"
exit 1

It works as intendet until it has to start the python script aka. this line:

screen -S foo -X python /home/developer/Test/

when running it in my normal shell i get:

sh: 1: cannot create /home/developer/Test/test.log: Permission denied

MY Questions:

  1. I understand the cause of the Permission denied case (works with sudo) but how do i give Permissions and more interestingly, to whom do i give the Permissions to? (python? | screen? | myuser?)

  2. Is the line to create a new instance in which the script runs correct like that?

  3. Can u think of a better way to execute a python script which has to run night and day but is start and stoppable and doesn't block the shell?


To answer your questions:

  1. You should not need to use sudo at all if the proper user/group is set on the scripts.
$ chmod 644 <user> <group> <script name>
  1. The line creating the new instance does not look correct, it should be more like:
screen -S foo -d -m /usr/bin/python /home/Developer/Test/

While using full path to the python exec; remove useless preceding line: screen -dmS foo sh

  1. Screen is more than adequte to prefer such tasks.

Other problems in your script:

  1. Add a shebang to the python script (eg. #!/usr/bin/python)

  2. Typo on line 20 of should be $Status, not $Staus

  3. You may need to initially create test.log before executing your script (eg. touch test.log)