2

Im trying to run a script after bootup, when the desktop is shown. The script is rather simple:

#!/bin/bash

mkdir /home/pi/Desktop/test

So in other words: This script is creating af folder "Test" on the Desktop after bootup. It is important that it is run after bootup and not before "anything else" (it is part of a bigger script of mine). How can i achieve this on the Raspberry Pi 4, running Raspbian 10 (buster)?

Ingo
  • 42,961
  • 20
  • 87
  • 207
jrn6270
  • 143
  • 2
  • 10

2 Answers2

2

You should use systemd with its power of handling dependencies. You have to know what services need the directory you want to create on bootup and create the directory before these services. systemd services are managed with Unit files. There you can use the options Before= and After=. For an early generic target I have used sysinit.target as example. Maybe it's better to use the basic.target? You may have a look at the System bootup process to find a better target for you use case. Create a new service with:

rpi ~$ sudo systemctl --force --full edit create-dir.service

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

[Unit]
Description=Create test directory
After=local-fs.target

[Service]
Type=oneshot
RemainAfterExit=yes
User=pi
ExecStart=/bin/mkdir -p /home/pi/Desktop/test

[Install]
WantedBy=sysinit.target

Enable and monitor the new service with:

rpi ~$ sudo systemctl enable create-dir.service
rpi ~$ systemctl status create-dir.service

You can check what services depend on your new service to ensure that it is executed before you need the directory:

rpi ~$ systemctl list-dependencies --reverse create-dir.service

This is an example to just execute a simple system command. To answer a question from a comment, it is in general possible to execute any program/script with a service as long as it can be executed from the command line. But it also depends on what services and environment the script needs to run. If the script needs a network connection or a graphical environment (e.g. starting an internet browser) you cannot use the sysinit.target of course. It's to early. With creating a service it is always the main work to find the correct environment to run the script smoothly. For example it is not needed to run the graphical terminal program lxterminal. It only makes things more complex then needed if your script doesn't need a graphical environment. Everything you can run in lxterminal you can also run direct with a service. Have a look at the tag systemd-unit for examples.

Ingo
  • 42,961
  • 20
  • 87
  • 207
1

There are a few different ways to achieve this, but you can try the following:

  1. Create a file for your script in the /etc/init.d/ directory. For this example let's say your script is the following:

/etc/init.d/myscript

  1. Next, make the script executable:

sudo chmod 755 /etc/init.d/myscript

  1. Now register the script to run at startup using this command:

sudo update-rc.d /etc/init.d/myscript defaults

At this point your script should be ready to automatically run during startup.
I hop this helps.

Steven
  • 39
  • 2