Unix & Linux: Cause a script to execute after networking has started? (7 Solutions!!)
Unix & Linux: Cause a script to execute after networking has started?
The Question: I am relatively new to systemd and am learning its architecture.
Right now, I'm trying to figure out how to cause a custom shell script to run.
This script needs to run after the networking layer has started up.
I'm running Arch, using systemd as well as netctl.
To test, I wrote a simple script that simply executes ip addr list > /tmp/
ip.txt. I created the following service file for this script.
(/etc/systemd/system/test.service)
[Unit]
Description=test service
[Service]
ExecStart=/root/test.script
[Install]
WantedBy=multi-user.target
I then enabled the script with,
systemctl enable test
Upon restarting, the script does indeed run, but it runs prior to the network
being started. In other words, the output in ip.txt displays no IPv4 address
assigned to the primary interface. By the time I login, the IPv4 address has
indeed been assigned and networking is up.
I'm guessing I could alter the point at which the script runs by messing with
the WantedBy parameter, but I'm not sure how to do that.
Could someone point me in the right direction?
Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful
== This solution helped 9 people ==
You can use After in [Unit] section to define a service that should be started
before your service starts. For example if you are using NetworkManager, you
can make your service start after NetworkManager is started.
[Unit]
Description=test service
After=NetworkManager.service
== This solution helped 4 people ==
I'm guessing I could alter the point at which the script runs by
messing with the WantedBy parameter
That will have the opposite effect of what you want. From man systemd.unit:
WantedBy=, RequiredBy=
[...] A symbolic link is created in the .wants/ or .requires/
directory of each of the listed units when this unit is installed by
systemctl enable. This has the effect that a dependency of type
Wants= or Requires= is added from the listed unit to the current
unit.
Based on this, we can see the proper unit option is "Wants" or "Requires";
based on the description of those, "Requires" is probably correct, with the
addition of "After" to ensure not only that the networking service be run, but
that it run before this unit.
None of the unit options, AFAIK, can include the stipulation that a started
perquisite must have completed, or reached a certain point (networking is
probably a daemon service), only that it start first. With this in mind, you
may want to make your script Type=forking and throw in a healthy delay (say 30
seconds), or some kind of an exit-on-success loop including a delay, to make
sure that you have a DHCP lease first.
With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user Zhenxiao Hao (https://unix.stackexchange.com/users/58397), user Tobu (https://unix.stackexchange.com/users/2729), user phoops (https://unix.stackexchange.com/users/61083), user Pavel Simerda (https://unix.stackexchange.com/users/60296), user Michael Hampton (https://unix.stackexchange.com/users/20805), user Jeff Schaller (https://unix.stackexchange.com/users/117549), user goldilocks (https://unix.stackexchange.com/users/25985), user fdmillion (https://unix.stackexchange.com/users/65884), user Colo Host (https://unix.stackexchange.com/users/123751), user Braiam (https://unix.stackexchange.com/users/41104), and the Stack Exchange Network (http://unix.stackexchange.com/questions/126009). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Unix & Linux: Cause a script to execute after networking has started? (7 Solutions!!)», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.