LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Debian (https://www.linuxquestions.org/questions/debian-26/)
-   -   Launch script at startup (https://www.linuxquestions.org/questions/debian-26/launch-script-at-startup-4175639939/)

black-clover 10-08-2018 12:29 AM

Launch script at startup
 
What would be the simplest way to launch a script during the boot or desktop startup process in debian?

The script in question consists of a single command (ifup wlan0) to connect the wifi and requires root privileges.
My system is debian testing, slim login manager, and jwm as window manager.

I have tried the following:

A- creating the .service way with systemd. It starts the script, but turns it off at desktop login, can't figure out why.
B- using the startup command in Jwm, but it fails I assume because launching ifup requires root privileges.
C- put the command in the .bashrc file. same as the above
D- place the script in the /etc/rc5.d directory.

nothing worked so far. suggestions?

ondoho 10-08-2018 01:19 AM

are you using network-manager?
is there the network manager applet in your system tray?

i'm sure jwm has its own autostart mechanism, did you check that out?

black-clover 10-08-2018 12:09 PM

I'm not using network manager, the point of my script is actually to avoid using it.
The script itself works fine, I can launch it manually from the terminal.
What I need is a way to execute the script when the machine boot.
jwm autostart is B in my list, but it does not seem to work.

michaelk 10-08-2018 12:44 PM

You can create a systemd service to run a rc.local script which will by default run with root privileges. I know other distributions have a rc-local service.

https://www.itechlounge.net/2017/10/...l-in-debian-9/

black-clover 10-08-2018 02:57 PM

michaelk,

I'll try what's suggested in the link, but wonder if there shouldn't be a simpler way.

michaelk 10-08-2018 03:05 PM

A cron job that executes at startup i.e. @boot as a system cron task should also work.

black-clover 10-08-2018 03:51 PM

Thanks everyone for the input.

In case anyone else needs to know, this is what eventually worked.

a- created a /etc/rc.local file
b- chmoded it 755
c- in the rc.local file I put:

Code:

#!/bin/sh -e
# This script is executed at the end of each multiuser runlevel

/path/to/my/script 

exit 0


dedec0 09-05-2021 03:04 PM

Quote:

Originally Posted by black-clover (Post 5912554)
Thanks everyone for the input.

In case anyone else needs to know, this is what eventually worked.

a- created a /etc/rc.local file
b- chmoded it 755
c- in the rc.local file I put:

Code:

#!/bin/sh -e
# This script is executed at the end of each multiuser runlevel

/path/to/my/script 

exit 0


Of everything showed in this thread, this solution seems the most simple, to me. I want a script to be run at every boot, and it must be after all partitions in fstab are mounted.

I have read that rc.local is deprecated, and systemd should be used, instead. But systemd is much strange, i think. I do not want to run a service. I just want to run a script, and it will finish quickly. It only requires the file systems to be mounted.

Right now, i am using Debian 9. But it should be soon updated to Debian 11. So, the solution should work on both.

What do you say?

ondoho 09-06-2021 12:10 AM

Quote:

Originally Posted by dedec0 (Post 6281903)
I have read that rc.local is deprecated, and systemd should be used, instead. But systemd is much strange, i think. I do not want to run a service. I just want to run a script, and it will finish quickly. It only requires the file systems to be mounted.

It's a bullet you will have to bite evtl., unless you deliberately start using non-systemd distros.
There's a good primer on writing your own services here.
Not every service runs as a daemon, it can be made to run only once at bootup. IIRC that's even the default. Dependency checking is even easier than with simple shell scripts, as it only requires a configuration option to be set.

FWIW, on Debian 9, /etc/rc.local Should(tm) still work.

dedec0 09-06-2021 03:44 AM

This archlinux documentation you sent has so many warnings of missing things, that i prefer not to use it.

I found that systemd has several types of units to have. One of them is "service". A command, or a set of commands, is not among the unit types listed in this page.

So, how should a unit be created for running a set of commands? I am having trouble to find a full example of this.

How to write, in the new service file, the idea of "run this after fstab things are mounted"?

A strange fact in my system: Teamviewer has the file /etc/systemd/system/teamviewerd.service , which should be enough (for what i have read about systemd), but it also has /etc/systemd/system/multi-user.target.wants/teamviewerd.service . Why?

Some people i found say to "look at the units installed in your system to see examples". I see no example of what i want: a unit created for running a set of commands.

shruggy 09-06-2021 05:59 AM

Quote:

Originally Posted by dedec0 (Post 6282030)
I see no example of what i want: a unit created for running a set of commands.

Does
Code:

grep -lrw oneshot /usr/lib/systemd/{system,user}/*.service
display anything for you? Like
Code:

/usr/lib/systemd/system/grub-common.service

ondoho 09-07-2021 12:03 AM

Quote:

Originally Posted by dedec0 (Post 6282030)
This archlinux documentation you sent has so many warnings of missing things, that i prefer not to use it.

I think you misunderstand the concept of a wiki, and also have never heard of archwiki's reputation.
But hey, suit yourself.
¯\_(ツ)_/¯

I'm also beginning to understand why you always have such problems solving your problems, and require full handholding even after 10+ years of Linux... if you continually dismiss the best resources.

dedec0 09-07-2021 04:11 PM

Quote:

Originally Posted by shruggy (Post 6282060)
Does
Code:

grep -lrw oneshot /usr/lib/systemd/{system,user}/*.service
display anything for you? Like
Code:

/usr/lib/systemd/system/grub-common.service

No:

Code:



2021.09.07 18:04:53 [  0] eu@compo: /usr/lib/systemd

$ grep -lrw oneshot /usr/lib/systemd/{system,user}/*.service

grep: /usr/lib/systemd/system/*.service: Arquivo ou diretório
inexistente

/usr/lib/systemd/user/systemd-tmpfiles-clean.service

/usr/lib/systemd/user/systemd-tmpfiles-setup.service


2021.09.07 18:05:20 [  2] eu@compo: /usr/lib/systemd

$  cat /usr/lib/systemd/user/systemd-tmpfiles-clean.service

#  SPDX-License-Identifier: LGPL-2.1-or-later

#

#  This file is part of systemd.

#

#  systemd is free software; you can redistribute it and/or
modify it

#  under the terms of the GNU Lesser General Public License as
published by

#  the Free Software Foundation; either version 2.1 of the
License, or

#  (at your option) any later version.


[Unit]

Description=Cleanup of User's Temporary Files and Directories

Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)

DefaultDependencies=no

Conflicts=shutdown.target

Before=basic.target shutdown.target


[Service]

Type=oneshot

ExecStart=systemd-tmpfiles --user --clean

SuccessExitStatus=DATAERR

IOSchedulingClass=idle

Slice=background.slice


2021.09.07 18:05:40 [  0] eu@compo: /usr/lib/systemd

$  cat /usr/lib/systemd/user/systemd-tmpfiles-setup.service

#  SPDX-License-Identifier: LGPL-2.1-or-later

#

#  This file is part of systemd.

#

#  systemd is free software; you can redistribute it and/or
modify it

#  under the terms of the GNU Lesser General Public License as
published by

#  the Free Software Foundation; either version 2.1 of the
License, or

#  (at your option) any later version.


[Unit]

Description=Create User's Volatile Files and Directories

Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)

DefaultDependencies=no

Conflicts=shutdown.target

Before=basic.target shutdown.target

RefuseManualStop=yes


[Service]

Type=oneshot

RemainAfterExit=yes

ExecStart=systemd-tmpfiles --user --create --remove --boot

SuccessExitStatus=DATAERR


[Install]

WantedBy=basic.target


2021.09.07 18:05:50 [  0] eu@compo: /usr/lib/systemd

$

How did you find that thing of "one shot"? I see 'ExecStart' with one command. To make it execute more than one command, will i always have to put them scripts, and save these scripts somewhere in the $PATH?

dedec0 09-19-2021 08:32 PM

Quote:

Originally Posted by dedec0 (Post 6282460)
No:

[...]

How did you find that thing of "one shot"? I see 'ExecStart' with one command. To make it execute more than one command, will i always have to put them scripts, and save these scripts somewhere in the $PATH?

No answers or comments?

michaelk 09-19-2021 10:19 PM

As a FYI the systemd rc-local.service still exists in debian 11.


All times are GMT -5. The time now is 09:40 AM.