How to run a script on system startup

I’m new to Manjaro.
What the best way is for having a script run after system startup? I want to mount a network drive and start a webserver.

I have seen that the desktop offers a configuration menu ‘Session and Startup’ where I can specify applications for autostart, with a trigger for autostarting them.
I have then created a shell script and added it to the autostart section, triggered by restart (although I would have preferred ‘on system start’, but that’s not in the list).
What I see now is: The script seems to get executed during the shutdown phase of a restart, not during the startup phase. It does not get executed when I first shutdown the system and start it again. So my script will never become effective.

I have solved my problem by adding an ‘@reboot’ entry to root’s crontab. This works fine, but I’m not totally happy with it because I have read somewhere that @reboot is deprecated.

So my questions are:

  • Can I have a script run at system startup by configuring it somewhere in the settings GUI?
  • If not: Where is the recommended hook?

The preferred way on a systemd system is to write a systemd unit for your script. This is normal way to start a service/script/program/daemon on boot.

Cron is deprecate in favor of systemd timers. But you probably don’t need them anyway.

This GUI options are can only be used to start a service/script/program/daemon with user permissions after a Human user login in. This is not “at startup”.

You only need to edit text files, no need for a GUI. Use your favorite terminal text editor for this.

Something to read.
https://wiki.archlinux.org/index.php/Systemd#Writing_unit_files
https://www.freedesktop.org/software/systemd/man/systemd.service.html

Also check out example 3. Which is a good starting point for s systemd unit that need to start a script. But depending on what your script does, oneshot might not work.
https://www.freedesktop.org/software/systemd/man/systemd.service.html#Examples

You should start your websever with the normal provided systemd service. Maybe add your own script service as a requirement.

However mounting is done via systemd.mounts. Check out


2 Likes

I add my scripts to .xprofile. It is extremely simple and does not require systemd (I move between systemd & non-systemd based distros).

Here’s an example invocation of a script call from my .xprofile.

wallpaper changer script

~/.local/share/scripts/wallpaper.sh &

There are some downsides on using .xprofile . For example a script in .xprofile is only exectued after a Human user logs in. This is for me not the same as on boot or on start up.
Another is that a script in .xprofile will only be executed if you log in via a GUI or via startx. And of course it works only on X11. It will not work on Wayland.

The script seems to get executed during the shutdown phase of a restart, not during the startup phase

If you want script to run during startup, change the trigger to ‘on login’ instead of ‘after restart’ (like the other items that run after system start)

or find and edit the launcher - ~/.config/autostart/[script].desktop
change RunHook=3 to RunHook=0

@nikgnomic:
I have tried the login trigger, and it doesn’t work. I suppose there is a simple explanation. I have added the login trigger to user root, because the things I want to accomplish need privileges. But after boot, the desktop belongs to a plain user. So there is no user login for root.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.