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)?

4
  • 1
    Why not add that to your bigger script? If folder does not exist create else do nothing?
    – CoderMike
    Mar 17 '20 at 20:04
  • 1
    @CoderMike The bigger script of mine already have that function, so no worries. But thanks for the tip anyways :-)
    – jrn6270
    Mar 18 '20 at 8:02
  • 3
    Does this answer your question? Execute script on start-up Mar 18 '20 at 9:45
  • 1
    @DmitryGrigoryev No, I have tried that. But the solution from Ingo worked for me. Thanks otherwise.
    – jrn6270
    Mar 18 '20 at 10:05
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.

3
  • Thank you very much! This worked perfectly for me. Now one last question: Is it possible in the service to do something like this: "ExecStart=lxterminal -e /home/pi/Desktop/create-dir-script.sh"? In other words: In the service to start the lxterminal automatically at startup and then make the terminal run the script?
    – jrn6270
    Mar 18 '20 at 8:51
  • @jrn6270 I have updated the answer with the last paragraph. If running into problems with your script just make a new question.
    – Ingo
    Mar 18 '20 at 9:55
  • I've added a new comment. Hope you can help (y)
    – jrn6270
    Mar 18 '20 at 12:53
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.

3
  • 4
    Do NOT advise people to use obsolete SysV methods
    – Milliways
    Mar 17 '20 at 20:41
  • 1
    In addition, this is unlikely to work properly unless the script adheres to the Debian SysV init script format. Which there is no point in learning about since, again, this methodology is now obsolete pretty much everywhere, including all common place OS's used on the Pi.
    – goldilocks
    Mar 18 '20 at 16:11
  • Hey all, thanks for the feedback. This was my method, but it has been some time since I've needed to achieve this. Would the ideal method be using systemd?
    – Steven
    Mar 20 '20 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.