2

I have 2 apps, lets call them /home/pi/app1 and /home/pi/app2

If I type into console sudo /home/pi/app1 & sudo /home/pi/app2 both apps execute without issue

When I add the same line to /etc/rc.local , only the first app gets executed

Why is the second app not getting executed?

Next I tried making a shell file /home/pi/run.sh which contains

#!/bin/bash
sudo /home/pi/app1 &
sudo /home/pi/app2 &

And in rc.local

#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

sudo /home/pi/run.sh &

exit 0

This time, no app gets executed, why?

I made sure I added a chmod +x to each file and each app

If I just call sudo /home/pi/run.sh & both apps run just fine

How can I make this work??!

4 Answers 4

2

To elaborate the answer from @Milliways, using /etc/rc.local is deprecated. Many distributions including Debian doesn't have it anymore. You should not use it. The reason is that it cannot be completely be emulated by systemd so there may be side effects which prevent programs to run properly. Please have a look at Compatibility with SysV. In particular the last sentence is important:

Note that there are some areas where systemd currently provides a certain amount of compatibility where we expect this compatibility to be removed eventually.

With using up to date Raspbian versions like Buster you will loose more and more support of rc.local. So you should use a systemd unit file to start your programs. To be more flexible I suggest to run one service for each application. Here is a very simple Unit for the first application as entry point. Create a new service with:

rpi ~$ sudo systemctl --force --full edit myapp1.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=My first application
After=multi-user.target

[Service]
ExecStart=/home/pi/app1

[Install]
WantedBy=multi-user.target

Enable and monitor it with:

rpi ~$ sudo systemctl enable --now myapp1.service
rpi ~$ systemctl status myapp1.service

I don't believe that this will run on the first attempt because many questions have to be answered about your applications but you do not tell us anything about them. Within a Unit file you have many options to define the needed environment so it is always possible to make the program running if it already runs on the command line. At least these questions have to be answered:

  1. Does the app only runs onetime, terminating after a short time, or does it stay in the background running as service?
  2. Because it is running on the command line, does it need tu run with user pi?
  3. Does it need environment settings like setting environment variables?
  4. Does it need other programs to be started before running the application?
  5. Are there conditions to meet before running the app, e.g. database access, network connections, mounted devices etc.?
  6. Is there any graphical output or only text output on a console?
  7. Maybe other conditions for your app?

If you like you can improve your question with needed information and I will adjust my answer.

1

rc.local doesn't tolerate errors.

rc.local doesn't provide a way to intelligently recover from errors. If any command fails, it stops running. Therefore, never run your startup services by that. More.

However, maybe the first command dropped or declare some warning/error to the system hence the second command would never run.
Check rc.local logs out:

cat /var/log/syslog

The scripts included in rc.local (and init.d) are run by root and then some of them drop privileges or change users. So, thanks for adding command with sudo there. Also you can add /usr/bin/sudo instead of sudo like /usr/bin/sudo sudo /home/pi/run.sh &.


In addition, maybe your first app needs more time for running or something like that so you can set some delay between running commands:

sudo /home/pi/app1 &  
sleep 10
sudo /home/pi/app2 &  

The first command run and after 10 seconds, it's time for the second one.

3
  • I tried adding a delay, but it literally works when I input both one one line without any delay in console
    – Mi Po
    Commented Jan 31, 2020 at 23:48
  • would something like this work to record any output errors: sudo /home/pi/run.sh &> /tmp/log I tried, but /tmp/log doesn't even get created.
    – Mi Po
    Commented Jan 31, 2020 at 23:48
  • @MiPo Hello and welcome to this community. -- Yes you can get the log/output of a command by that but you can not see what happened with the rc.local. On /var/log/syslog it tells you what exactly happened. Commented Jan 31, 2020 at 23:52
0

This is another of the hundreds of similar questions asked on this site - rather than repeating I suggest you search or read https://www.raspberrypi.org/documentation/linux/usage/rc-local.md.

Some points to consider:-

  1. Do not use sudo - rc.local already runs as root.
  2. systemd (used on Jessie, Stretch & Buster) starts processes asynchronously and rc.local can only be reliably used on some processes,
3
  • 1
    I have searched this specific question about running 2 apps concurrently and have not seen any posts on it, and I do not understand what is different about the command I type in to execute apps and why this same command fails in rc.local
    – Mi Po
    Commented Jan 31, 2020 at 23:35
  • 1
    Your Question is unanswerable without details of the apps. Simply rc.local is obsolete and you need to use other methods. This almost certainly has nothing
    – Milliways
    Commented Jan 31, 2020 at 23:42
  • 2
    Why does the command sudo /home/pi/app1 & sudo /home/pi/app2 run in command line but not in rc.local ?
    – Mi Po
    Commented Jan 31, 2020 at 23:45
0

Resolved

I was missing cd /home/pi in /etc/rc.local so it was not running from the correct directory which led to missing files in the app

1
  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year.
    – Ingo
    Commented Feb 8, 2020 at 7:52

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.