0

I have a raspberry pi 4 and i connected a sixfab LTE HAT to it. I wrote a program that works manually (sudo python3 server2.py). I wrote a simple shell script and put it to rc.local that has the following lines in it

cd /home/pi/Desktop
sudo python3 server2.py

When i check the rc-local.service, i get the message that when i try to send something with the LTE there is a timeout error "connection timed out"

sserver.connect((TCP_IP_SERVER,TCP_PORT_SERVER))

Note that i have to use sudo because i configured the raspberry to socket connection with wwan0 instead of the wifi using:

sserver.setsockopt(socket.SOL_SOCKET,25, str("wwan0"+'\0').encode('utf-8'))

As i mentioned previously, the program runs manually. Can i ask if you have any comments or ideas on the subject?

1
  • 2
    Change the /etc/rc.local lines to python3 /home/pi/Desktop/server2.py & that & is super important to fork the process into the background. Also you don't need sudo as rc.local runs as root. Also it does NOT run with the same environment as your regular pi shell (watch out for file names without full paths).
    – Dougie
    Commented Jul 8, 2020 at 14:59

2 Answers 2

5

One comment or idea to the subject is: don't use /etc/rc.local anymore!

Debian/Raspbian/Raspberry Pi OS uses systemd instead of deprecated SysV since years as init system to start and manage services. rc.local is a leftover from SysV and only emulated by systemd, but with serious limitations. Have a look at Compatibility with SysV.

You should use a systemd Unit file to start your service. You can find many examples here on this site.

2

As @Ingo has said in his answer, systemd is indeed a valid and current method for starting a program at boot time. I also agree that /etc/rc.local should be avoided - this is not so much a matter of opinion as documented fact (see, for example):

The man page for systemd-rc-local-generator states, “Support for /etc/rc.local is provided for compatibility with specific System V systems only. However, it is strongly recommended to avoid making use of this script today, and instead provide proper unit files with appropriate dependencies for any scripts to run during the boot process.

However, there is a valid and current method for doing what you want that you may find simpler, easier and equally effective as long as you follow a few simple rules. This method is called cron, and here's how it can work in your case:

1. Open a crontab file:

From the command line, enter crontab -e, OR if you need your script to run with root privileges, enter sudo crontab -e. If you don't need elevated privileges, you should use the former.

In either case, after entering this command, your terminal will open the crontab file in an editor (nano is a good choice if prompted to select an editor).

2. Edit the crontab file:

In your case, enter the following line at the bottom of the crontab file:

@reboot ( /bin/sleep 10; /usr/bin/python3 /home/pi/Desktop/server2.py ) >> /home/piDesktop/cronjoblog.txt 2>&1

Let's break this down:

  • @reboot instructs cron to run the following commands at boot time

  • sleep 10 tells cron to sleep/wait for 10 seconds before proceeding to the next command

  • /usr/bin/python3 /home/pi/Desktop/server2.py says use python3 to run the script in server2.py

  • >> /home/piDesktop/cronjoblog.txt 2>&1 redirects >> the stdout and stderr streams 2>&1 to a text file on your desktop

3. Save the crontab file, exit the editor and reboot to test:

If you're using nano as your editor, controlo to save, acknowledge the proposed filename with return, and controlx to exit nano and return to the command line. Let us know if you run into any issues.

Addendum: The simple rules for using cron are based on two facts:

  1. cron does not inherit your environment variables

    Consequently, preface commands with an absolute path. A related symptom is that any output from a program that runs successfully from the command line is "lost". Lost error messages in particular are not helpful! Therefore: redirect all messages to a log file.

  2. cron does not know what resources are required to run your program

    Network and file services may not be available when cron is started. If not, cron doesn't know what to do! Prefacing commands with sleep allows the system to get these services started before cron calls on them.

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.