[Software] [Solved] How do I run a script at startup?

New to Debian (Or Linux in general)? Ask your questions here!
Post Reply
Message
Author
diningroom
Posts: 13
Joined: 2025-02-03 02:31
Been thanked: 2 times

[Software] [Solved] How do I run a script at startup?

#1 Post by diningroom »

I have a script in /usr/local/sbin that I would like to run everytime the PC (and Debian) starts.

I have ...

-rw-r--r-- 1 root root 185 Feb 2 19:23 startup.service

in /etc/systemd/system]

Contents of that file are:

===

# startup.service

[Unit]
Description=Run the run-at-startup.sh script systemd service.

[Service]
Type=simple
ExecStart=/bin/sh /usr/local/sbin/run-at-startup.sh

[Install]
WantedBy=multi-user.target


===

# ls -al /usr/local/sbin/run-at-startup.sh
-rwxr-xr-x 1 root root 56 Feb 2 19:33 /usr/local/sbin/run-at-startup.sh


And the contents of that startup script are ...

===
# less /usr/local/sbin/run-at-startup.sh
#!/bin/sh


echo Starting ....

logger Starting ...

===



What am I doing incorrectly?

thx.

:linked:
Last edited by diningroom on 2025-02-06 19:36, edited 2 times in total.

User avatar
kent_dorfman766
Posts: 594
Joined: 2022-12-16 06:34
Location: socialist states of america
Has thanked: 77 times
Been thanked: 78 times

Re: [Software] How do I run a script at startup?

#2 Post by kent_dorfman766 »

you have to enable your new service using

Code: Select all

systemctl enable myservice
and reboot
or

Code: Select all

systemctl enable myservice
followed by

Code: Select all

systemctl daemon-reload
if you don't want to reboot

diningroom
Posts: 13
Joined: 2025-02-03 02:31
Been thanked: 2 times

Re: [Software] How do I run a script at startup?

#3 Post by diningroom »

OK, I ran ...

===
systemctl enable startup.service

===

And there was no output from that command, so I presume it had no issues?

OK, I just rebooted the computer.



OK, I look at at the output from journal, and I went to the end of that output, I do not see an entry from the run-at-startup.sh script.

fwiw, I can run that script interactively, and it works quite well.

So, at this point, please allow me to take a step back and ask once again ...


How do I run a "/usr/local/sbin/run-at-startup.sh" script with the computer starts up?

diningroom
Posts: 13
Joined: 2025-02-03 02:31
Been thanked: 2 times

Re: [Software] How do I run a script at startup?

#4 Post by diningroom »

> OK, I look at at the output from journal, and I went to the end of that output, I do not see an entry from the run-at-startup.sh script.

>fwiw, I can run that script interactively, and it works quite well.

That is what baffled me.

The script runs OK interactively, but not upon reboot.


There is no error message in the systemd journal about the script. (I grep'd the output of the journal command)

It is like that script does not exist and any errors associated with it cannot be reported.

How do i get systemd to pay attention to a script I want systemd to run when I start my computer? Or, at least, complain if systemd has an issue with a task it is presented and has an issue with?

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2162
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 171 times
Been thanked: 246 times

Re: [Software] How do I run a script at startup?

#5 Post by Hallvor »

Please check the status and/or issues

Code: Select all

systemctl status startup.service
If it fails, this should give you a hint of what's wrong

Code: Select all

journalctl -u startup.service
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

diningroom
Posts: 13
Joined: 2025-02-03 02:31
Been thanked: 2 times

Re: [Software] How do I run a script at startup?

#6 Post by diningroom »

Before I continue, I want to say, many thanks for the able assistance being provided. I am learning stuff. :)

OK, I shut the laptop down for a couple days while other life priorities took precedence. But I'm back.

When I started up the laptop, I now see that the "run-at-startup.sh" script, specified by the startup.service file has run twice, about a half minute apart.

So I venture into the /etc/systemd/system directory, and see my original startup.service file. (looks like I need to enter systemd to the browser spell check ...)

I also see a link in the /etc/systemd/system/multi-user.target.wants directory back to the startup.service file mentioned above.

OK, is the reason I am seeing two runnings of the run-at-startup.sh script due to one time being run in single-user mode, and the second time when Debian switches to multi-user mode?


I tried changing the Type to oneshot, i.e.,

===

[Service]
Type=oneshot
ExecStart=/bin/sh /usr/local/sbin/run-at-startup.sh

===

But I still see two runnings of the script..

I need it to run only once.

What should I look at next?

Also, as requested earlied, here's the status ...

===

# systemctl status startup.service
○ startup.service - Run the run-at-startup.sh script systemd service.
Loaded: loaded (/etc/systemd/system/startup.service; enabled; preset: enabled)
Active: inactive (dead) since Tue 2025-02-04 18:12:12 EST; 5min ago
Process: 651 ExecStart=/bin/sh /usr/local/sbin/run-at-startup.sh (code=exited, status=0/SUCCESS)
Main PID: 651 (code=exited, status=0/SUCCESS)
CPU: 19ms

Feb 04 18:12:10 channels systemd[1]: Starting startup.service - Run the run-at-startup.sh script systemd service....
Feb 04 18:12:12 channels systemd[1]: startup.service: Deactivated successfully.
Feb 04 18:12:12 channels systemd[1]: Finished startup.service - Run the run-at-startup.sh script systemd service..

===

Note that the "exit 0" is intended. The script runs a command or two or three, then exits witha return code of 0.

CwF
Global Moderator
Global Moderator
Posts: 3262
Joined: 2018-06-20 15:16
Location: Colorado
Has thanked: 69 times
Been thanked: 288 times

Re: [Software] How do I run a script at startup?

#7 Post by CwF »

diningroom wrote: 2025-02-04 23:22 But I still see two runnings of the script..

I need it to run only once.

What should I look at next?
I ran across a note to myself on the subject...
but if you can wait for, and have a user login:
Create an autostart target at ~/.config/systemd/user/autostart.target
Create service files at ~/.config/systemd/user/<service name>.service
Mottainai

diningroom
Posts: 13
Joined: 2025-02-03 02:31
Been thanked: 2 times

Re: [Software] How do I run a script at startup?

#8 Post by diningroom »

CwF wrote: 2025-02-05 05:28
diningroom wrote: 2025-02-04 23:22 But I still see two runnings of the script..

I need it to run only once.

What should I look at next?
I ran across a note to myself on the subject...
but if you can wait for, and have a user login:
Create an autostart target at ~/.config/systemd/user/autostart.target
Create service files at ~/.config/systemd/user/<service name>.service
Thanks for that. At this point, it may be a day or two or three before I can return to this test laptop.

But one question I do have to ask at this point.

In FreeBSD I have the simple @reboot line in crontab.

But here in Debian, that line seems to be greeted with an error message.

Instead, here in Debian, I have to wade through man pages full of facts, but short of information of how to solve a problem (can you say man systemd.unit? i knew you could.)

Now, I have used Linux off and on since the late 1990's. But lately Linux seems to have become more opaque to those who need to solve simple problems, i.e., run a script with a laptop is booted.

I apologize for my venting here.

But really, why doesn't crontab seem to like the @reboot line? Such a simple solution to a problem that has been around for years.

Complexity and a wall of man pages in systemd compared to a simple line in crontab?

I mean, realy.
.

CwF
Global Moderator
Global Moderator
Posts: 3262
Joined: 2018-06-20 15:16
Location: Colorado
Has thanked: 69 times
Been thanked: 288 times

Re: [Software] How do I run a script at startup?

#9 Post by CwF »

diningroom wrote: 2025-02-05 06:04 But one question I do have to ask at this point.

In FreeBSD I have the simple @reboot line in crontab.

But here in Debian, that line seems to be greeted with an error message.
Maybe,
viewtopic.php?t=161135

Edit:Never mind, I think I misinterpreted the question.
Mottainai

diningroom
Posts: 13
Joined: 2025-02-03 02:31
Been thanked: 2 times

Re: [Software] How do I run a script at startup?

#10 Post by diningroom »

CwF wrote: 2025-02-05 06:33 Edit:Never mind, I think I misinterpreted the question.
No worries.

I thank you for taking the time to read my comment.

User avatar
kent_dorfman766
Posts: 594
Joined: 2022-12-16 06:34
Location: socialist states of america
Has thanked: 77 times
Been thanked: 78 times

Re: [Software] How do I run a script at startup?

#11 Post by kent_dorfman766 »

I hesitate to mention it, but systemd also still supports the rc.local startup script if you enable it by enabling the rc-local service.
make sure the /etc/rc.local script is executable and put stuff there that needs to be sequentially executed AFTER all the other system services are done.

I user /etc/rc.local to create some fifos, and set kernel params through the /sys interface.

diningroom
Posts: 13
Joined: 2025-02-03 02:31
Been thanked: 2 times

Re: [Software] How do I run a script at startup?

#12 Post by diningroom »

kent_dorfman766 wrote: 2025-02-05 07:32 I hesitate to mention it, but systemd also still supports the rc.local startup script if you enable it by enabling the rc-local service.
make sure the /etc/rc.local script is executable and put stuff there that needs to be sequentially executed AFTER all the other system services are done.

I user /etc/rc.local to create some fifos, and set kernel params through the /sys interface.

Thanks for that.

That is the solution I needed.

I created a /etc/rc.local file and marked it executable.

In rc.local I put the script I want to run upon boot-up, with the full pathname.

It works!

I would have gone with that solution first, instead of systemd, but when I did a "man" search for rc.local and rc, nothing was returned.

Thanks again.

lindi
Debian Developer
Debian Developer
Posts: 646
Joined: 2022-07-12 14:10
Has thanked: 2 times
Been thanked: 129 times

Re: [Software] How do I run a script at startup?

#13 Post by lindi »

diningroom wrote: 2025-02-04 23:22 OK, is the reason I am seeing two runnings of the run-at-startup.sh script due to one time being run in single-user mode, and the second time when Debian switches to multi-user mode?
The original service file you posted works just fine on my Debian 12 system. I even rebooted to single user mode and exited that.

diningroom
Posts: 13
Joined: 2025-02-03 02:31
Been thanked: 2 times

Re: [Software] [Solved] How do I run a script at startup?

#14 Post by diningroom »

Thanks for that follow-up.

Yes, that first service file I posted worked for me also, but it resulted in the script running twice each time I booted.

I wish I knew why.

Maybe when i finish setting up this server I'll fire up a test system and try to track things down, and also learn more about systemd.

In any case, I have a solution that does all I need with rc.local.

Thanks.

diningroom
Posts: 13
Joined: 2025-02-03 02:31
Been thanked: 2 times

Re: [Software] [Solved] How do I run a script at startup?

#15 Post by diningroom »

fwiw ...

I just remembered another command I should have used besides the man page comment to find out about rc.local ...
apropos rc.local
rc-local.service (8) - Compatibility generator and service to start /etc/rc.local during boot
systemd-rc-local-generator (8) - Compatibility generator and service to start /etc/rc.local during boot
man 8 rc-local.service

gave me...

===

Support for /etc/rc.local is provided for compatibility with specific System V systems only. However, it is strongly recommended to avoid making use of this script today, and instead provide proper unit files with appropriate dependencies for any scripts to run during the boot process. Note that the path to the script is set at compile time and varies between distributions.

===

So, apparently a temporary solution.

"unit file" - yeah, that indecipherable service.unit man page.

As opposed to the simplicity of rc.local.

So it is looking like I may need to fire up that test server sooner than later.

diningroom
Posts: 13
Joined: 2025-02-03 02:31
Been thanked: 2 times

Re: [Software] [Solved] How do I run a script at startup?

#16 Post by diningroom »

... and if I may be so forward to proffer on bit of advice to the Debian team I have utmost respect for ...?

Don't demean rc.local to some, soon to be go away, compatibility ~old feature.~

Celebrate it, and promote it for it's feature ... simplicity towards a goal.

Why should i have to wind up in man system.unit and deal with that mess of complexity when all I want to do is run a script upon startup?

arzgi
Posts: 1831
Joined: 2008-02-21 17:03
Location: Finland
Has thanked: 1 time
Been thanked: 107 times

Re: [Software] [Solved] How do I run a script at startup?

#17 Post by arzgi »

I had a few years ago Raspberry Pis to run some automation systems. rasbian supported rc.local, but could not get my program running at every boot. So went to rasberry questions, and they sugggestend systemd units.

It is not so hard, use your favourite searh engine, and you find a lot of good tutorials.

lindi
Debian Developer
Debian Developer
Posts: 646
Joined: 2022-07-12 14:10
Has thanked: 2 times
Been thanked: 129 times

Re: [Software] [Solved] How do I run a script at startup?

#18 Post by lindi »

diningroom wrote: 2025-02-06 21:29 Thanks for that follow-up.

Yes, that first service file I posted worked for me also, but it resulted in the script running twice each time I booted.

It ran only once for me. How did you determine how many times it was run?

eamanu
Debian Developer
Debian Developer
Posts: 43
Joined: 2020-07-12 21:37
Has thanked: 4 times
Been thanked: 5 times
Contact:

Re: [Software] [Solved] How do I run a script at startup?

#19 Post by eamanu »

@Best_Threads

diningroom
Posts: 13
Joined: 2025-02-03 02:31
Been thanked: 2 times

Re: [Software] [Solved] How do I run a script at startup?

#20 Post by diningroom »

lindi wrote: 2025-02-07 15:49
diningroom wrote: 2025-02-06 21:29 Thanks for that follow-up.

Yes, that first service file I posted worked for me also, but it resulted in the script running twice each time I booted.

It ran only once for me. How did you determine how many times it was run?

Thanks for the follow-up. Apologies for the delay in replying. I'm focused upon setting up the server for now.

OK, how did I know it ran twice?

My standard run-at-startup.sh script begins its life with two items in it.


The first is a logger entry that says the script is starting.
/bin/logger -t startup "entering $0 ..."
The second is emailing the dmesg ...
/bin/dmesg | mail -s "channels - starting" root

When I looked at the log file via journalctl, I saw those two things occurring twice.

Using rc.local, I see them occurring only once.


As I said earlier, I'm focused on getting this server up and running now. Once I do that, I'll be circling back to try again with my service file and learning more about systemd.



... and fwiw... as a result of this learning sequence, I will be changing the "-t startup" option in the logger command to "-t mystartup"

That would make it easier to search for with less noise. :D


thx again for your reply.

Post Reply