Rc.local to start web app?

A couple years ago I created a web app that I ran on a Pi 3. I added

sudo /home/pi/jobs/RotaryJobs &

to the /etc/rc.local file and it still works fine.

I have a new Pi 4 and rc.local no longer works. I can run the app by getting in the proper directory and typing

sudo ./RotaryJobs &

but I can’t start it with rc.local.

The Pi 3 has not been running since Covid shut down the Rotary club and the “apt-get update” returns say most of the repositories are no longer available so I am wondering if rc.local no longer works. Any suggestions for starting the app would be greatly appreciated.

What version of Raspberry OS are you using?

Desktop or console app?

It’s a web app, so a console app.

I should pay better attention :slight_smile:

Latest RPi OS is more heavily systemd based.

These steps should run your program on startup:

  1. edit a new file:

sudo nano /etc/systemd/system/rotary.service

  1. copy the following into the editor and save:
[Unit]
Description=Rotary Club
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/jobs
ExecStart=/home/pi/jobs/RotaryJobs
Restart=on-failure
KillSignal=SIGINT
TimeoutStopSec=60

[Install]
WantedBy=multi-user.target
  1. Enable the service:

sudo systemctl enable rotary.service

  1. Make sure the network service is running:

sudo systemctl enable systemd-networkd-wait-online.service

On reboot, your program should be running.

1 Like

I am running the latest version of Raspbian GNU/Linux 10 (buster)

I followed John’s example exactly just updating the app name to my latest app. It does not work. It may work for a regular app, but I found I had to run as root to use port 80. I switched to User=root, but it still does not work. I even tried putting an ampersand, &, after the app name the way I do when I run it in Terminal and it did not help.

Ideally, I would like to run the program in /opt under root so users won’t find it and corrupt anything.

Back to Square 1. Now the program no longer runs when I enter “sudo ./LynxMaster &”

Any message when you run manually?

Here is a simpler version that I used for an Ubuntu system running Xojo web:

[Unit]
Description=Web server
After=network.target

[Service]
WorkingDirectory=/srv/disweb
ExecStart=/srv/disweb/disweb
Restart=on-failure

[Install]
WantedBy=multi-user.target

followed by:
sudo systemctl enable disweb.service

The server (and resource files) are installed in /srv/disweb.

@Dean_Davidge
Make sure the app folder and all of its contents are owned by root if you’re going to use

User=root

You may also want to use

Group=root

FWIW, I did manage to just get a standalone app running on a Raspberry Pi 4 using a similar config to what is explained above. Just in case there are any differences, I’ll also document what I’m using here.

  1. mkdir -p /opt/example

  2. cd /opt/example

  3. copy the entire build folder of your app into this folder (For this example, we’ll call it TestApp)

  4. set permissions:
    sudo chown -R root:root TestApp
    sudo chmod -R 700 TestApp

  5. make a SystemD config file:
    sudo nano /lib/systemd/system/TestApp.service

  6. Set the contents of the file as follows:

    [Unit]
    Description=Your App Description Goes Here
    After=network.target

    [Install]
    WantedBy=multi-user.target

    [Service]
    nice=5
    ExecStart=/opt/example/TestApp/TestApp --port=80
    PrivateTmp=true
    User=root
    Group=root

  7. CTRL-X and Save Changes

  8. sudo systemctl enable TestApp

  9. sudo systemctl start TestApp

  10. Access the app in a browser.

3 Likes

I followed Greg’s example to the letter changing only TestApp to LynxMaster. It is not working. Looking at the system log after rebooting the Pi, LynxMaster does not appear. When I start it using line 9 from Greg’s example I get this in the log

Feb 18 15:11:43 raspberrypi systemd[1]: Started Lynx Web Server.
Feb 18 15:11:43 raspberrypi systemd[1348]: LynxMaster.service: Failed to determine user credentials: No such process
Feb 18 15:11:43 raspberrypi systemd[1348]: LynxMaster.service: Failed at step USER spawning /opt/ezmeter/LynxMaster/LynxMaster: No such process
Feb 18 15:11:43 raspberrypi systemd[1]: LynxMaster.service: Main process exited, code=exited, status=217/USER
Feb 18 15:11:43 raspberrypi systemd[1]: LynxMaster.service: Failed with result ‘exit-code’.

When I go to the folder with the app and enter “sudo ./LynxMaster &” I get a blank screen in the browser (instead of the “This site can’t be reached” I get otherwise) and the system log shows entries from the app.opening and the first line of the session.opening. The next line of the session.opening calls the app method to connect to the database, but that does happen. When I recompiled for port 8080, it opens properly when I manually start the app. A Pi web app I wrote in 2018 needed a setcap command to allow it to use port 80. Should that have been in Greg’s code?

How can I solve the user credentials issue? I had set a root password earlier so I can SSH into the Pi with WinSCP. Would that have caused the problem?

An interesting thing I noticed. When I started the 8080 version of the program, it started with PID 931. When I tried to kill it, it said there was no such process. Kill 932 killed it. That’s not the way it used to work.

How about trying an empty app instead and just see if the idea works on your pi.

I’m not familiar with an empty app, so I tried it on a different rPi. On my first attempt, I had set a new root password and used WinSCP to copy the program into a folder under /opt. This time, I used a fresh install of the rPi OS and did not set a root password. Systemctl launched the program and reported success. Syslog revealed that the app started but the next step was to get a folderitem for the SQLite database and write the file path to syslog. That did not happen.

I am not going to worry about it now as we have decided to use Postgres instead of SQLite and that code has not been written yet.

I know very little about Linux, but it appeared to me that changing the root password affected systemctl’s ability to validate the root user. But after changing the root password, I experienced the same result as before changing it. I now have no idea why one rPi works and the other does not. Rather worrisome if this is to be a commercial product.