Hi All!
I am trying to run a script to check if our PaperCut User Client application is actually running.
I was using the below script on my machine and it returned the results as expected.
*#!/bin/sh
SERVICE=PCClient
if ps ax | grep -v grep | grep -v $0 | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
fi*
But when I put this into JAMF as a policy to run the script, it falls over and returns this.
Script result: grep: Support/JAMF/tmp/Check: No such file or directory
grep: if: No such file or directory
grep: PCClient: No such file or directory
grep: is: No such file or directory
grep: running: No such file or directory
PCClient is not running
I assume this is because of where it is trying to run the file from. So I did some digging on here to try and find a script I could add to, to force it to run as the logged-in user.
I found the one below and tried to edit it to work, but it gets unexpected end of file, and cannot work out why (i am new to mac and scripting so apologies if it's an obvious oversight!)
#!/bin/bash
loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u "$loggedInUser")
if [[ "$loggedInUser" != "root" ]] || [[ "$loggedInUID" -ne 0 ]]; then
cat << EOF > /private/tmp/script.sh
#!/bin/bash
service=PCClient
if ps ax | grep -v grep | grep -v $0 | grep $SERVICE > /dev/null
then
echo "$SERVICE service running, everything is fine"
else
echo "$SERVICE is not running"
exit 0
EOF
else
echo "No user logged in. Can't run as user, so exiting"
exit 0
fi
if [ -e /private/tmp/script.sh ]; then
/bin/chmod +x /private/tmp/script.sh
/bin/launchctl asuser "$loggedInUID" sudo -iu "$loggedInUser" "/private/tmp/script.sh"
sleep 2
echo "Cleaning up..."
/bin/rm -f "/private/tmp/script.sh"
else
echo "Oops! Couldn't find the script to run. Something went wrong!"
exit 1
fi
All help gratefully received.
Matt