Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

improving a daemon script

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
11 posts • Page 1 of 1
Author
Message
DaggyStyle
Watchman
Watchman
User avatar
Posts: 5969
Joined: Wed Mar 22, 2006 6:57 am

improving a daemon script

  • Quote

Post by DaggyStyle » Fri Oct 23, 2020 10:15 am

Greetings,

I have simple script that runs in the background (launches by an openrc script)m the script stays up using a endless loop with sleep of 1 sec and reacts to signals sent via udev events (add and remove).
ideally, I'd like to remove the service and relay only on udev events.
the main issue is that the add event needs to run several commands a few seconds after udev finished handling the add event.

any ideas how I can achieve it? I've tried running fork from within the script when I called it from the udev event handler but it seems that udev waits until all the children of the script it runs to end before it continues.
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Top
DaggyStyle
Watchman
Watchman
User avatar
Posts: 5969
Joined: Wed Mar 22, 2006 6:57 am

  • Quote

Post by DaggyStyle » Sat Oct 24, 2020 10:08 am

one possibility is to add a sysfs entry which will run <path to script> foo when the udev rule does /bin/echo foo > <path to sysfs entry>
not sure if it will work. thought? I know it inst that safe kernel wise
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Top
GDH-gentoo
Advocate
Advocate
User avatar
Posts: 2111
Joined: Sat Jul 20, 2019 7:02 pm
Location: South America

Re: improving a daemon script

  • Quote

Post by GDH-gentoo » Sat Oct 24, 2020 3:19 pm

DaggyStyle wrote:ideally, I'd like to remove the service and relay only on udev events.
the main issue is that the add event needs to run several commands a few seconds after udev finished handling the add event.
I don't understand. An udev rule that matches the event you are interested in and contains RUN="your-script" does not work?
Top
Hu
Administrator
Administrator
Posts: 24389
Joined: Tue Mar 06, 2007 5:38 am

  • Quote

Post by Hu » Sat Oct 24, 2020 4:26 pm

That worked fine, once. Modern udev is very specific about how they don't want you running "long running" jobs. At least for systemd-udevd, I think they actually take steps that make this not work. I don't know if udev under openrc, or eudev under openrc, have the same problem. The canonical answer from the systemd people is to use a udev rule that triggers a systemd service to do the real work. That won't work under openrc, where there is no systemd service to trigger.

DaggyStyle: how exactly did you make the script fork?
Top
javeree
Guru
Guru
Posts: 465
Joined: Sun Jan 29, 2006 5:17 pm

  • Quote

Post by javeree » Sat Oct 24, 2020 4:37 pm

I believe the best way to start a long running script under udev is RUN="echo myscript | at now"
(required atd daemon)
Top
GDH-gentoo
Advocate
Advocate
User avatar
Posts: 2111
Joined: Sat Jul 20, 2019 7:02 pm
Location: South America

  • Quote

Post by GDH-gentoo » Sat Oct 24, 2020 7:29 pm

Hu wrote:That worked fine, once. Modern udev is very specific about how they don't want you running "long running" jobs. At least for systemd-udevd, I think they actually take steps that make this not work.
Yeah, as far as I can tell, both systemd-udevd and eudev's udevd kill the spawned process if it runs for too long, but "too long" is a time interval of configurable duration that defaults to 3 minutes.
javeree wrote:I believe the best way to start a long running script under udev is RUN="echo myscript | at now"
(required atd daemon)
I don't think that will work; as far as I can tell the value assigned to RUN is not parsed by a shell, so the pipe character might not work as expected. RUN="/bin/sh -c 'echo myscript | at now'" on the other hand might, but I don't see at the moment why simply calling the script won't work.

EDIT to clarify: What I'm expecting the script spawned by the udev rule to do is running the per-event "several commands" referred to in the OP, not a long-lived process. I understand that the long-lived process spawned by an OpenRC service script is what the OP wants to get rid of.
Top
DaggyStyle
Watchman
Watchman
User avatar
Posts: 5969
Joined: Wed Mar 22, 2006 6:57 am

  • Quote

Post by DaggyStyle » Sun Oct 25, 2020 6:04 am

GDH-gentoo wrote:
DaggyStyle wrote:ideally, I'd like to remove the service and relay only on udev events.
the main issue is that the add event needs to run several commands a few seconds after udev finished handling the add event.
I don't understand. An udev rule that matches the event you are interested in and contains RUN="your-script" does not work?
it runs well but as explained above, the device takes a few minutes to function after the add rule was matched.
the script waits for 5'6 minutes for a specific "signal", if within that timeframe the specific "signal" is received, I continue with the next cmds, if not, it bails out.
Hu wrote:That worked fine, once. Modern udev is very specific about how they don't want you running "long running" jobs. At least for systemd-udevd, I think they actually take steps that make this not work. I don't know if udev under openrc, or eudev under openrc, have the same problem. The canonical answer from the systemd people is to use a udev rule that triggers a systemd service to do the real work. That won't work under openrc, where there is no systemd service to trigger.

DaggyStyle: how exactly did you make the script fork?
tried &, nohup and even openrc pre_up, all of them worked.
javeree wrote:I believe the best way to start a long running script under udev is RUN="echo myscript | at now"
(required atd daemon)
thanks for the sugggestion, will look at it.
GDH-gentoo wrote:
Hu wrote:That worked fine, once. Modern udev is very specific about how they don't want you running "long running" jobs. At least for systemd-udevd, I think they actually take steps that make this not work.
Yeah, as far as I can tell, both systemd-udevd and eudev's udevd kill the spawned process if it runs for too long, but "too long" is a time interval of configurable duration that defaults to 3 minutes.
javeree wrote:I believe the best way to start a long running script under udev is RUN="echo myscript | at now"
(required atd daemon)
I don't think that will work; as far as I can tell the value assigned to RUN is not parsed by a shell, so the pipe character might not work as expected. RUN="/bin/sh -c 'echo myscript | at now'" on the other hand might, but I don't see at the moment why simply calling the script won't work.

EDIT to clarify: What I'm expecting the script spawned by the udev rule to do is running the per-event "several commands" referred to in the OP, not a long-lived process. I understand that the long-lived process spawned by an OpenRC service script is what the OP wants to get rid of.
the main issue is that udev waits for the child, that is want I need to fix, e.g. udev runs the script and continues while the script continues in the background.
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Top
ff11
l33t
l33t
User avatar
Posts: 665
Joined: Mon Mar 10, 2014 10:24 pm

  • Quote

Post by ff11 » Sun Oct 25, 2020 1:31 pm

DaggyStyle wrote:...
the main issue is that udev waits for the child, that is want I need to fix, e.g. udev runs the script and continues while the script continues in the background.
Yes, but what if udev calls a script just_trigger.sh and you call inside it the real script, childless (using something like ( script_that_wait_and_do_things.sh & ), or disown, yes, bash ^_^), which will wait and do the job, but it won't block the normal return from the just_trigger.sh script and let udev go its way. You can even do a check (inside script_that_wait_and_do_things.sh) if you want only one instance to run and working at a time.
| Proverbs 26:12 |
| There is more hope for a fool than for a wise man that are wise in his own eyes. |
* AlphaGo - The Movie - Full Documentary "I want to apologize for being so powerless" - Lee
Top
DaggyStyle
Watchman
Watchman
User avatar
Posts: 5969
Joined: Wed Mar 22, 2006 6:57 am

  • Quote

Post by DaggyStyle » Sun Oct 25, 2020 1:37 pm

ff11 wrote:
DaggyStyle wrote:...
the main issue is that udev waits for the child, that is want I need to fix, e.g. udev runs the script and continues while the script continues in the background.
Yes, but what if udev calls a script just_trigger.sh and you call inside it the real script, childless (using something like ( script_that_wait_and_do_things.sh & ), or disown, yes, bash ^_^), which will wait and do the job, but it won't block the normal return from the just_trigger.sh script and let udev go its way. You can even do a check (inside script_that_wait_and_do_things.sh) if you want only one instance to run and working at a time.
not sure I follow, if I have script a.sh, inside a.sh calls func fa like this:
fs &
then udev will wait. but I change is to:
( fa & ), a.sh will exit and fa will run in the background?
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Top
ff11
l33t
l33t
User avatar
Posts: 665
Joined: Mon Mar 10, 2014 10:24 pm

  • Quote

Post by ff11 » Sun Oct 25, 2020 1:42 pm

DaggyStyle wrote:...
not sure I follow, if I have script a.sh, inside a.sh calls func fa like this:
fs &
then udev will wait. but I change is to:
( fa & ), a.sh will exit and fa will run in the background?
I never tested it with functions, just with real scripts, but the idea is this:
In ( script_that_wait_and_do_things.sh & ), the (..) creates a child process, and & creates a grandchild process. When the child process dies, the grandchild process is inherited by init.
| Proverbs 26:12 |
| There is more hope for a fool than for a wise man that are wise in his own eyes. |
* AlphaGo - The Movie - Full Documentary "I want to apologize for being so powerless" - Lee
Top
DaggyStyle
Watchman
Watchman
User avatar
Posts: 5969
Joined: Wed Mar 22, 2006 6:57 am

  • Quote

Post by DaggyStyle » Sun Oct 25, 2020 2:17 pm

ok, I'l try this suggestion, hope it works.

thanks.
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Top
Post Reply

11 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic