Run script on startup

My Raspberry Pi uses Manjaro Server, but I cant get it to autorun my sh file on startup. I’ve tried crontabs and editing all sorts of etc files but nothing seems to work. Whats the recommended/official way to run a script on startup?

Share your script and make sure it is executable → chmod +x ./path_to_script

There is no “official” way. I recommend putting such scripts in /etc/X11/xinit/xinitrc.d. This will autostart it on Xorg startup and most display manager (specially lightdm) source this directory. Also see Autostarting - ArchWiki.

1 Like

What is this? Do you mean Minimal, which means headless, without a GUI?

However, create a systemd service for your Script and enable it.

https://wiki.archlinux.org/title/Systemd#Writing_unit_files

1 Like

A systemd service of the type oneshot.

To get an idea of how you could look at

3 Likes

Sorry for late response,
I made a file called autostart.service in etc/systemd/system with the contents

[Unit]
Description=Start scripts
StopWhenUnneeded=yes
Before=multi-user.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/home/pi/startup.sh

[Install]
WantedBy=multi-user.target

and then enabled it with sudo systemctl enable autostart.service
which didnt error.
However, when I restarted it didnt run my script.
I ran /home/pi/startup.sh to see if it does work, and it worked just fine.
It is an executable, and here are the contents (not sure if its necessary);

#!/bin/bash

tmux new-session -d -s 0 'cd jesterbot && python main.py'
tmux new-session -d -s 1 'cd stealthybot && python3.10 main.py'
tmux new-session -d -s 2 'cd raspberry-dashboard && npm start index.js'

Any ideas?

Yes, I mean minimal

Your service fails because the script is in your home.
The paths in your script is not fully qualified which makes the script fail.

Create a folder

sudo mkdir -p /etc/systemd/scripts

move you script to the folder and change the path in the service file.

Example - a service I use to sync manjaro repo

[Unit]
Description=Sync Manjaro repository

[Service]
Type=oneshot
ExecStart=/etc/systemd/scripts/rsync-manjaro.sh

[Install]
WantedBy=multi-user.target

Check the jounrnal.
For example

journalctl -b -u autostart.service

Personally, I would create 3 services for your 3 programs. Do theses programs really need a terminal? Why not just run them directly. Any output is saved in the journal. By default a system service is run as root, this might be a problem. The oneshot type is not optimal because it will stop after the script is finished and all processes it created get killed. RemainAfterExit does something else, the processes are still killed.

For example rtorrent needs to run in a terminal, my systemd service looks like this

[Unit]
Description=rTorrent Daemon
After=network.target network-online.target
Requires=ovh_torrent_ban.service network-online.target

[Service]
Type=forking
User=xabbu
ExecStart=/usr/bin/tmux new-session -s rtorrent -n rtorrent -d rtorrent
ExecStop=/bin/bash -c "/usr/bin/tmux send-keys -t rtorrent:rtorrent.0 C-q && sleep 5"
WorkingDirectory=/home/xabbu/

[Install]
WantedBy=multi-user.target

The type depends on the application. However, I would strongly suggest you try to create services without tmux.

1 Like

I moved the script but it still didnt work.
Output to journalctl -b - u autostart.service:

-- Journal begins at Sat 2020-10-03 21:56:00 UTC, ends at Sat 2022-03-26 17:50:>
Mar 26 17:49:36 pi systemd[1]: Starting Start scripts...
Mar 26 17:49:36 pi systemd[1]: autostart.service: Deactivated successfully.
Mar 26 17:49:36 pi systemd[1]: Finished Start scripts.

Ps I removed the RemainAfter, Before and StopWhenUnneded

I am using tmux because these scripts dont just run in the background; the entire purpose of my pi is to run these scripts and they may error in which case I need to open the tmux, run some commands and restart the process. I like that tmux works like that. I’ll try changing the type to simple

After that, all processes create by this service are got killed. Totally normal and expected. You need to change your approach.

It will be the same in this case, if you start the script.

You need to get rid of the script and start tmux direly in the service.

Okay, I will take your approach - is the ExecStop, After, or Requires necessary?
Should it be forking or simple?

depends on the application. I would start with simple

No.

After, is usually a good idea, so the service is not started to early in the boot process. If network is needed, you might want at least After=network.target . If an IP on the interface is required, you want Requires=network-online.target . But After=network.target is often a good choice.

autostart.service:


[Unit]
Description=Start scripts
After=network.target
Requires=network-online.target

[Service]
Type=simple
ExecStart=/usr/bin/tmux new-session -d -s 0 'cd /home/pi/jesterbot && python main.py'

[Install]
WantedBy=multi-user.target

Tmux still said no sessions found when I tried to attach to the session.

This is the journal, and im not sure why it says it Deactivated when its type simple

-- Journal begins at Sat 2020-10-03 21:56:00 UTC, ends at Sat 2022-03-26 18:11:>
Mar 26 18:11:31 pi systemd[1]: Started Start scripts.
Mar 26 18:11:31 pi systemd[1]: autostart.service: Deactivated successfully.

To clarify, I did systemctl daemon-reload

You start the service as root, so you have to attach to tmux as user as well.
Or you start the service as user.

I don’t even know if it’s possible to attach to a tmux session inside systemd. Why do you need tmux at all?

Try Type=forking

I am using tmux because these scripts dont just run in the background; the entire purpose of my pi is to run these scripts and they may error in which case I need to open the tmux, run some commands and restart the process without having to restart the whole pi. I like that tmux works like that

I seems that xabbu has it working, so it should be possible

if there is a better method than creating a unit, i’d love to know

[pi@pi ~]$ journalctl -b -u autostart.service
-- Journal begins at Sat 2020-10-03 21:56:00 UTC, ends at Sat 2022-03-26 18:24:>
Mar 26 18:23:21 pi systemd[1]: Starting Start scripts...
Mar 26 18:23:22 pi systemd[1]: Started Start scripts.
Mar 26 18:23:24 pi systemd[1]: autostart.service: Deactivated successfully.
Mar 26 18:23:24 pi systemd[1]: autostart.service: Consumed 1.395s CPU time.

with type forking

But then you could restart the service if they fail.

I just feel like its less interactive yk