0

I am trying to run a script which ultimately runs a pagekite python file. The Pagekite offers tunnelling service for localhost to be publicly accessible. My aim is to run the pagekite.py when the Pi is booted and the website is up and running. I am trying to run the script with root permissions. I tried using two approaches:

  1. Using the # crontab -e with root privileges and adding @reboot bash -x /home/username/myscript.sh in crontab file
  2. Adding the script file in /etc/init.d and creating symlink with sudo ln -s /etc/init.d/myscript.sh /etc/rc.d/.

However none of these approaches tend to work. Please note, I have kept the python file in the same location as of the script (Earlier, I even tried adding the cd command to the destination folder containing pagekite.py in myscript.sh script.). Also sudo is needed in the prefix for running the python script. After attempting the reboot, nothing happens and the web page is unreachable.Apart from that, is it possible to run the script automatically and display the kite status in the terminal, like the one we get if we manually run the pagekite script ? Check the image below for what I mean by "kite status in terminal window"

Contents of myscript.sh file

#!/bin/sh
sudo python3 pagekite.py 80 furball.pagekite.me 

Contents of sudo crontab -e

@reboot sudo bash -x /home/username/Desktop/myscript.sh

The py file and sh are both present in same directory.

Source: pagekite.net

2
  • Have you successfully run your script from the command line?
    – Seamus
    Commented Nov 24, 2021 at 2:08
  • @Seamus Yes. The website is operational once the raspberry pi boots.
    – DaSnipeKid
    Commented Nov 24, 2021 at 4:39

5 Answers 5

1

You mentioned that the contents of the myscript.sh file are :

#!/bin/sh sudo python3 pagekite.py 80 furball.pagekite.me

I suggest that you also add full paths for all the above, including python.

I.e. :

#!/bin/sh
sudo /usr/bin/python3 <full pagekite.py path>/pagekite.py 80 <full furball.pagekite.me path>/furball.pagekite.me

At any case, run also a :

cat /var/log/syslog|grep cron 

that might give you a clue on what goes wrong...

1
  • Thanks a ton ! This worked, I think adding the paths for each file in myscipt.sh and ` sudo crontab -e` enabled each one of them to work as desired.
    – DaSnipeKid
    Commented Nov 23, 2021 at 17:11
2

Your first approach may not work because

  • bash is not fully qualified and cron's path may not include /usr/bin

  • -X is not a valid bash option.

  • there may be a permission problem preventing cron from accessing your script

  • The network may not be up yet when the job is executed. You could try to delay it a little bit with bash -c 'sleep 60 && /home/username/myscript.sh'

Your second approach will not work because:

  • /etc/rc.d is not a valid SysV name. You probably meant /etc/rc.local or /etc/rc5.d.

  • SysV is not used anymore. SystemD (which took its place) tries to keep the compatibility by running some of the SysV scripts, but it cannot run all of them correctly, and it cannot run them in the correct order. In your case, you most probably need network connection before your script starts.

Look into how SystemD units are written, and write one with After=network-online.target in it.

2

To answer your headline question: How to run a script (pointing to a python file) with root permission at startup in Raspberry Pi OS?:

I don't know what you tried because your question is unclear to me, but if root privileges are required to run a script at start/boot on RPi, here's how to do that:

$ sudo crontab -e

This will present the root crontab in your default editor. Once in the editor, to run a script at start/boot, use the @reboot facility:

@reboot /path/to/your/script

That will do it. If you're having issues beyond that, you will have to provide more details if you want help. The answer you referenced is misleading at best because, like you, the OP provided no details of the script he was trying to run.

Likewise, we can't help you with your bash -X because the -X option applies to a filter specification used in bash's word completion feature, and it looks out-of-place as you've used it in your question

Other than that, the instructions above, as applied to what you say you've tried look like this in your root crontab file:

@reboot bash /home/username/myscript.sh

When you reboot your system, it will most certainly execute the script at /home/username/myscript.sh with root privileges. Again, if you're not getting the result you hoped for, we need more information to help you.

Alternatively, as this seems slightly off-topic for Raspberry Pi, you might consider asking your question in another forum.

1
  • My deepest apologies. I changed the command from @reboot bash -x /home/username/myscript.sh in the question. This was a typo. Also, I have added the contents of .sh file and crontab -e in the question. By bash -x, the option -x stands for Print commands and their arguments as they are executed. . I got this information from man bash. What exactly I need is the command is not executed after reboot (which it should, because of @reboot).
    – DaSnipeKid
    Commented Nov 23, 2021 at 6:01
0

To run a script that point to a python script you can write an sh script

#!/bin/sh
python /path/of/pagekite.py

then in root crontab

@reboot /home/yourhome/yourscript.sh

For the specific case of pagekite... if you install pagekite from curl

  curl -s https://pagekite.net/pk/ |sudo bash

and not from package manager then add rule to ~/.pagekite.rc with the command:

    pagekite.py --add 80 http:foo.pagekite.me:8080 # A local webserver
    pagekite.py --add 22 ssh:foo.pagekite.me # Enable SSH tunneling

then in home write pagekite.sh

   #!/bin/sh
   /usr/local/bin/pagekite.py

remember to:

  chmod +x pagekite.sh

and in cron

  crontab -e

add the line

  @reboot /home/yourhome/pagekite.sh

and it's ok

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented May 18 at 18:26
0

I once worked on a commercial product that had to run a python/Qt GUI at startup, exactly what you are trying to accomplish. I remember implementing the SYSTEMD method from this link https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/ .

You open a file in systemd:

sudo nano /lib/systemd/system/sample.service

Then point to your file:

[Service]
 Type=idle
 ExecStart=/usr/bin/python /home/pi/sample.py

The only detail I noticed (that may be due to Raspberry Pi own speed) is that it takes a second for the file to open after the system is running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.