While loop while a policy is running?

Bko
New Contributor II

Is it possible to execute a while loop in a script while a policy is running? So while x policy runs, do something?

5 REPLIES 5

sdagley
Esteemed Contributor II

@Bko Off the top of my head you could have a script that runs at the beginning of your policy which creates a flag indicating your policy is running (e.g. touch /tmp/MyPolicyIsRunning) then spawns a process for your while loop that looks for that flag to keep running. At the end of your policy run a script that deletes the flag (e.g. rm /tmp/MyPolicyIsRunning)

Bko
New Contributor II

Oh that's a great idea!

Bko
New Contributor II
Off the top of my head you could have a script that runs at the beginning of your policy which creates a flag indicating your policy is running (e.g. touch /tmp/MyPolicyIsRunning) then spawns a process for your while loop that looks for that flag to keep running. At the end of your policy run a script that deletes the flag (e.g. rm /tmp/MyPolicyIsRunning)

Actually I'm not sure it'll work now. So what I'm trying to do is to have a while loop that checks the download progress of a large file in a policy. Even with your method, I think, the download would have to be complete before the loop starts or the loop starts before the download begins and would have to break.

The idea is as long as the policy is running, to check how much of the file has downloaded, compare it to the end file size, and output it within (replacing) the jamf helper window that is already up. I'm trying to keep cocoa dialogue or any other programs, out of it.

ricardtolosa
New Contributor III

You can try to put everything under a script.
You could first trigger the caching or download policy adding a & at the end so the script continues and add the while loop afterwards to monitor the download process.

#!/bin/zsh
jamf policy -event downloadMyBigPkg &

Once done you can do your checks and exit the script.

sdagley
Esteemed Contributor II

@Bko If you have a Script payload set to run "Before" it should execute before the download of any Package payloads in your policy. You would need to expand your logic in the spawned script to wait for the download to begin while the /tmp/MyPolicyIsRunning flag is in existence since the download may not have started when your script starts, but that flag indicates it's expected.