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.