2

I would like to refer to my previous post here: Autostarting a python script whenever it crashes to restart two python scripts on my raspberry pi (Stretch OS) if they crash.

The code of the shell script called constantrun.sh is as follows:

#!/bin/sh

PYTHONPATH=/home/pi/.local/lib/python2.7/site-packages
COMMAND1=python /home/pi/project/script1.py
COMMAND2=python /home/pi/project/script2.py

rm -f COMMAND1_failed; ("${COMMAND1[@]}"; touch COMMAND1_failed)&
rm -f COMMAND2_failed; ("${COMMAND2[@]}"; touch COMMAND2_failed)&

while true
do
        if [ -e COMMAND1_failed ]
        then
                # Restart Command1
                rm -f COMMAND1_failed; ("${COMMAND1[@]}"; touch COMMAND1_failed)&
        fi
        if [ -e COMMAND2_failed ]
        then
                # Restart Command2
                rm -f COMMAND2_failed; ("${COMMAND2[@]}"; touch COMMAND2_failed)&
        fi

        sleep 60
done

I ran the following commands in terminal:

sudo chmod +x constantrun.sh # to make the script executable

I then gave permission to my python scripts e.g. chmod +x /home/pi/project/script1.py

Next, to initialise the script, I used:

sudo sh constantrun.sh 

However, I got the following error messages:

/usr/bin/env: ‘python\r’: No such file or directory
/usr/bin/env: ‘python\r’: No such file or directory
constantrun.sh: 7: constantrun.sh: Bad substitution
constantrun.sh: 8: constantrun.sh: Bad substitution

How can I resolve these errors?

9
  • Thanks Goro. I have another error when I provide permission to my script: /usr/bin/env: ‘python\r’: No such file or directory /usr/bin/env: ‘python\r’: No such file or directory constantrun.sh: 7: constantrun.sh: Bad substitution constantrun.sh: 8: constantrun.sh: Bad substitution
    – Craver2000
    Commented Sep 16, 2018 at 13:49
  • Yes, I have python installed. Using which python returns: /usr/bin/python
    – Craver2000
    Commented Sep 16, 2018 at 13:54
  • It returns: ln: failed to create symbolic link '/usr/bin/python': File exists
    – Craver2000
    Commented Sep 16, 2018 at 13:57
  • 1
    Ah! probably your scripts contains CR characters., or dos_breaks. You can try ` vi +':wq ++ff=unix' script1.py; vi +':wq ++ff=unix' script2.py` or open the scripts in emacs then save the scripts again!
    – user88036
    Commented Sep 16, 2018 at 14:04
  • 1
    I'll try the emacs method shortly
    – Craver2000
    Commented Sep 16, 2018 at 14:17

2 Answers 2

2

Problems with \r are usually caused by using a Windows editor. Check your python scripts. The assignments to the command variables contain space. You use array syntax to refer to them, but don't define them as arrays. For PYTHONPATH to be useful, it must be exported.

There is a much simpler solution for this:

#!/bin/sh

export PYTHONPATH=/home/pi/.local/lib/python2.7/site-packages
COMMAND1="python /home/pi/project/script1.py"
COMMAND2="python /home/pi/project/script2.py"

(while true; do $COMMAND1; done &)
(while true; do $COMMAND2; done &)
2
  • I've changed my script to the code as suggested above but I still get the error: /usr/bin/env: ‘python\r’: No such file or directory /usr/bin/env: ‘python\r’: No such file or directory constantrun.sh: 7: constantrun.sh: Bad substitution constantrun.sh: 8: constantrun.sh: Bad substitution
    – Craver2000
    Commented Sep 17, 2018 at 12:59
  • The python\r error is caused by your Windows line endings. And where in all your information is a hint about env being used?
    – RalfFriedl
    Commented Sep 17, 2018 at 16:59
1

I think there are two important issues here:

First: You have to differentiate between the location of your python executable, and the location of the lib site-packages.

Your python site-packages are here:

/home/pi/.local/lib/python2.7/site-packages

But your executables are here:

/usr/bin

When you ran the command which python it told you that the executables are in /usr/bin

Your $PATH environment variable should contain paths that have executable files i.e.

$ echo $PATH /usr/bin:/usr/local/bin:/home/User/bin

If your executable is in another location other than your path, and you don't want to necessarily add that location to your path, then you can just create symbolic link. For example:

ln -s /path/to/executable /usr/bin/executable

Second: You had created your pythons in Windows, then you moved the scripts to Linux environment, probably your scripts contains CR characters, or dos breaks. You can clean the scripts from the CR characters as follows:

vi +':wq ++ff=unix' script1.py
vi +':wq ++ff=unix' script2.py

Or simply, open the scripts "script1.py", and "script2.py" in emacs and then save them again. This will remove the CR characters automatically.

2
  • I've just tried to save those scripts with emacs but I still get the error /usr/bin/env: ‘python\r’: No such file or directory /usr/bin/env: ‘python\r’: No such file or directory constantrun.sh: 7: constantrun.sh: Bad substitution constantrun.sh: 8: constantrun.sh: Bad substitution
    – Craver2000
    Commented Sep 17, 2018 at 12:54
  • Yes I did update python first with sudo apt-get update first and then sudo apt install --reinstall python python-apt python2.7-minimal. I also noticed that when I run these scripts individually with (ie. python script.py) on terminal, they worked. However, running them via shellscript produced the aforementioned error. `
    – Craver2000
    Commented Sep 17, 2018 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .