Shellcmd that doesn't block (non-blocking) and allows boot to continue
-
I'm running 22.01 dev snapshots on a 6100 and wanted to use the Shellcmd package to execute a script at boot that fixes the LEDs. So it needs to wait ~30s for the boot to complete otherwise the setting gets trampled by the
led_normalize()
at the bootup complete stage of the main rc script.I tried various invocations like
(sleep 30 ; echo 0 >/dev/led/blue3; )& #or /bin/sh -c 'sleep 30; echo 0 >/dev/led/blue3' & #or /root/my-led-setting-script.sh &
None of those worked- all of them blocked and halted the boot until the script finished. Is this a limitation of the way the Shellcmd package (or php?) calls out to the shell? I ended up punting and using a script in
/usr/local/etc/rc.d/
but even the Netgate docs recommend not doing that.I searched and didn't find much about this, except for r/PFSENSE: Non-blocking shellcmd? —which didn't get a definitive answer.
-
Now you know the PHP exec() is used, you can, look her https://www.php.net/manual/fr/function.exec.php and try out what has been proposed over there.
@luckman212 said in Shellcmd that doesn't block (non-blocking) and allows boot to continue:
a script in /usr/local/etc/rc.d/ but even the Netgate docs recommend not doing that.
But they mentioned it ;)
That method give you the most control. -
@gertjan Thanks. I'll paste the important bit below to save anyone else a click:
so I assume maybe the fix would be to make the shellcmd something like
/bin/sh -c 'sleep 30; echo 0 >/dev/led/blue3' >/dev/null
-
Still can't get it to work
If I enter an interactive php shell with
php -a
and then runexec("/bin/sh -c '/bin/sleep 5; echo 1 >/dev/led/amber3' >/dev/null &");
it does return control immediately and also turns on the LED after 5s. So that works.
But, the same command in the Shellcmd package doesn't work. I thought maybe it was because the string gets escaped e.g.
>
becomes>
, but when I tested a little patch, addinghtmlspecialchars_decode()
inside theexec()
it did not change anything... -
What about placing the commands in a script file "test.sh" :
#!/usr/local/bin/bash exec("/bin/sh -c '/bin/sleep 5; echo 1 >/dev/led/amber3' >/dev/null &");
Make the script file executable with
chmod +x test.sh
and then use
/root/test3.sh
as the early start command ?
-
@gertjan The top code snippet you wrote there has the wrong shebang (
exec()
is php, not bash). But aside from that, this is basically what I've done—put the commands in a script at/usr/local/etc/rc.d/
The problem with that is it requires manually copying the scripts, making sure they are backed up/restored etc. It would be nice to be able to use Shellcmd since everything is contained within the config.xml...
-
@luckman212 said in Shellcmd that doesn't block (non-blocking) and allows boot to continue:
The top code snippet you wrote there has the wrong shebang (exec() is php, not bash).
The PHP exec() function executes a command.
Not a binary command like 'sleep' or '/bin/sleep' (the command + path) but a command shell script file.
The 'language interpreter' of the script files is .... not PHP. It's bash. So the sheban is bash.@luckman212 said in Shellcmd that doesn't block (non-blocking) and allows boot to continue:
The problem with that is it requires manually copying the scripts, making sure they are backed up/restored etc. It would be nice to be able to use Shellcmd since everything is contained within the config.xml...
I understand. It would be best if everything was self contained in the config.xml.
I'm using several self written script files - PHP, bash, perl, python, that I placed in the /root/ folder - and should be put there manually when I need to rebuild the system.
Rebuilding my pfSense is something I do onces in a decade, or so. So I installed the Notes package (a must have) where I put the files, with comments, etc, so I will fin things when needed, in 2030 or so.I use also
-
@gertjan said in Shellcmd that doesn't block (non-blocking) and allows boot to continue:
Rebuilding my pfSense is something I do onces in a decade, or so. So I installed the Notes package (a must have) where I put the files, with comments, etc, so I will fin things when needed, in 2030 or so.
Why not install and use the "Filer" package? I mean that's what it's there for, isn't it? ;)
Also it saves everything in config.xml for later use and restores if after a new installation. So no need for manually backing up and writing every script down in the Notes section?Also
filer
andshellcmd
got pretty well hand-in-hand :)Cheers
\jens -
@jegr Never used the
backup
orfiler
packages although I've always been vaguely aware of them. Somehow I thought they were unmaintained for years and broken on recent versions of pfSense but I guess that's wrong. I'll check it out (as soon as pkg is fixed )