Need a script/batch/program that runs a command that won't be killed when the parent is killed
The scenario
I use Zabbix to monitor my servers and recently I wanted to add some more metrics for the Windows ones. For security reasons, I used Zabbix's User Parameter feature, but it limits the execution of external commands to about 3 seconds. After that, the command is forcibly killed.
I want to run some long run commands, so I used the trick from Zabbix's forum: run the command in the background, write the results to a file and use Zabbix to collect them.
This is rather easy under *nix thanks to the "&" operator, but there is no such support in Windows' shell. To make things worse, when Zabbix kills forcibly kill the cmd.exe
it used to evaluate the commands, all child processes die including the unfinished background tasks.
Thus I need something that can sever all the ties with its children so they won't be affected in the cascading kill.
What I've tried
-
start
andstart /B
- They do nothing as the child always die with the parent - WScript.Shell.Run as in invis.vbs from StackOverflow - Sometimes work. If the
wscript
process is forcibly killed as opposed to quitting on its own, the children will die as well. -
hstart
- similar results to invis.vbs -
At
command - This requires you to set an absolution time for the task to run as opposed to an offset, so the code would be quite messy due to the limited shell scripting capability of Windows. -
(Edit)
PsExec.exe
from the SysInternals suite - It uses a service to launch the command, so it is not affected by the kill; however, it prints some banner and log info to StdErr and there's no switch to disable this. When I use2>NUL
to redirect them, Zabbix reports an error.
After trying the above in different combinations, I noticed if I call hstart
from invis.vbs
, the command started by the former will be left alone as a parent-less process when invis.vbs
is killed.
However, since I need to redirect the output, the command I want to run is always in the form of cmd.exe /c ""command" "args"" >log
. The vbs also removes all the quotes, so I have to encode the command with self-defined escape sequences. The end result involves about five levels of escaping/quoting, which is almost impossible to maintain.
Anyone know any better solutions?
Some requirements
- Any bat/vbs/js/Win32 binary is acceptable
- Better not require multiple levels of escaping
- No .Net (including PowerShell) because it is not installed
Solution 1:
Separate whatever your lengthy script is supposed to be doing into its own script.
Using NSSM, make that script into a service; set its Exit action to Ignore.
Create a short batch file for Zabbix to start, its content is only one short line to start the service you created in the above step.
In step 2, alternatively, have the script end with exit code 0, and set the AppExit\0 action to Exit, which will gracefully instruct Windows' Service Manager to mark the service as stopped.
Solution 2:
Have you tried BeyondExec? It's very very similar to psexec, so I don't know if the same issue you have with psexec would apply, but it's worth a shot.