Running a script in Casper

nicampb
New Contributor

Hi, I am new to Casper and am having a problem running a shell script from Casper. The script works when run manually or when run from a JSS policy, but when run from Casper, it does not error out, but does not accomplish anything either. Below is the script. Any help would be much appreciated.

find $HOME -type f -name "prefs.js" -exec rm -f {} ;

4 REPLIES 4

jarednichols
Honored Contributor

When you run scripts in Casper, they generally happen in the context of the admin account, not the logged in user. Depending on how you're running it, $HOME is likely resolving to your admin account, not the logged in account.

The Generally Accepted™ way of doing it is to run it with a policy that has a login or logout trigger and use $3 instead. The jamf binary will resolve $3 to the logging in or out user.

If it's not run at login (or logout) and is instead run at the every15 trigger you can use something like

user=`ls -la /dev/console | cut -d " " -f 4`

and then call something like /Users/$user in your script.

FDA
New Contributor

so how would i use $3 to have a script run as the logged in user? All the script is doing is calling another script as the user and its being run as the admin since its being called by casper:

"sh /location/location/loginscript.sh&"

exit 0

nessts
Valued Contributor II

create a launch agent it will run as the user when he/she logs in.

<?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.my.login</string> <key>ProgramArguments</key> <array> <string>/path/toyour/loginscript.sh</string> </array> <key>RunAtLoad</key> <true/>
</dict>
</plist>

permissions should to be 644, file has to be owned by root:wheel lives in /Library/LaunchAgents

FDA
New Contributor

THanks but even though the plist file loads, the script never ends up running, any help would be appreciated