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
    Nov 24, 2021 at 2:08
  • @Seamus Yes. The website is operational once the raspberry pi boots.
    – DaSnipeKid
    Nov 24, 2021 at 4:39

3 Answers 3

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
    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
    Nov 23, 2021 at 6:01

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.