Disowning a process from a Casper script

Look
Valued Contributor III

Guys,

I am wanting to call a command from a script deployed by Casper and have the script so the policy can continue with the next task while leaving that process running.
Have tried all sorts of combinations of & () disown nohup and various other things.
Almost everything works fine in terminal but none of them seem to work when passed in a script from casper the script run always seems to wait for the spawned task to finishe before proceeding.

At the moment just trying to make a simple caffeinate with a time out, have used it a low in locally run scripts without issues.

The only way I can see that seems to work is to put the command into another casper policy and call that using a trigger, but it seems like a messy solution.

8 REPLIES 8

iJake
Valued Contributor

This is the format I use for spawning a subprocess in the background and have the script go on.

(INSERT COMMAND HERE) &

Look
Valued Contributor III

Yeah tried that didn't seem to work, the script will continue so I can perform other tasks within the script, but I am wanting to end the script completely the jamf run that called it and leave the task running.

For example call caffienate to force the machine awake for a couple of days or similar.

iJake
Valued Contributor

So you want to have a script in a policy kick off another process but be able to end the policy even though that subprocess might still be running?

Look
Valued Contributor III

@iJake Exactly what I want :)

iJake
Valued Contributor

If you don't need the result back from this subprocess in the policy I would just build a launchdaemon/agent to run this other script for you. It seems much easier than fighting with subprocesses. What is it this subprocess is doing if you can say?

Look
Valued Contributor III

Just conceptualising at the moment...
Currently just looking at caffeinate with a time out mostly.
Pretty much need to be calling it from Casper though as the idea is it will be tied to other Casper related events such as multiple policies that may be called during a maintenance window.
I could have something local already installed that would do what I wanted it to do I suppose, just like the cleanliness of having it all self contained.

iJake
Valued Contributor

I would envision maybe having a policy that runs every hour at check in that is set to check if certain conditions are met or these other scripts have finished their jobs that you could collect the information and kick off anything else either via policy or whatever. I just can't see how having a policy stay running that long is good. You'll have no idea if it's hung or anything as the logs won't submit until it finishes.

alexjdale
Valued Contributor III

I think a launchdaemon is the way to go. I can create and load a launchdaemon from /tmp to execute a script, so the launchdaemon will be deleted automatically on the next reboot and only run once. I've been doing this to kick off a reboot script so Casper can close out the policy and upload logs before the reboot.

I've also tried many options to spin off new threads, but Casper always waits for them to complete.

Sample code for a bash function to spin off a script:

echo '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.company.runscript</string>
    <key>UserName</key>
    <string>root</string>
    <key>ProgramArguments</key>
    <array>
        <string>/path/to/script.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>' > /tmp/restart.plist
launchctl load /tmp/restart.plist