image
image
Ticket Options
Question Details
TICKET ARCHIVE -> Force Shutdown, Logoff, Restart via AppleScript
Mickwilli - Jun 8, 2006 - 6:05 pm
image
image
HI

Does anyone know how to make your Mac force shutdown via an applescript. The one way I can think of is to try and kill "loginwindow". It would be fine of you could simply tell it to quit but it has be be forced to quit. Of course apple hasn't made a force quit command.

We need to shedule via a cron job to have this computer shutdown or something because if there are anoying applications open and wanting Passwords etc they cancel logout, this then means the script that we've made screws the computer over.
BjarneDM - Jun 10, 2006 - 7:52 am
image
image
I've been messing around with something similiar in bash. My need is to '
1) log all users except the administrator out
2) only allow the users working on a given day to log in

It's actually two scripts now:
1) one that forcefully logs a user out
2) one that makes the scheduling

lets take the forcefull log out first:
[14:46:54@bashScripts]$ cat killuser
#!/darwinports/bin/bash

ps -auxww -U ${1} \
| grep -E "loginwindow" \
| tr -s ' ' \
| cut -d ' ' -f 2 \
| xargs -n 1 sudo kill -KILL

sleep 20

ps -auxww -U ${1} \
| sed '1,1d' \
| tr -s ' ' \
| cut -d ' ' -f 2 \
| xargs -n 1 sudo kill -KILL
[14:47:01@bashScripts]$

and the other one:
[14:47:01@bashScripts]$ cat activeUser
#!/darwinports/bin/bash

declare -a UIDs=( $(niutil -list . /users uid | tr '\t' ':' | tr -d ' ') )
declare -a splittet
declare username

for idUid in ${UIDs[*]}
do
splittet=( $(echo ${idUid} | tr ':' ' ') )
if [ ${splittet[1]} -gt 500 ]
then
username=$(niutil -readprop . /users/uid=${splittet[1]} name)
case ${username} in
'bjarne')
;;
*)
# echo ${username}
# niutil -readprop . /users/name=${username} authentication_authority
/var/root/bashScripts/killuser ${username}
niutil -createprop . /users/name=${username} authentication_authority ";DisabledUser;;ShadowHash;"
# niutil -readprop . /users/name=${username} authentication_authority
;;
esac
fi
done

/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend

case "${*}" in
'all' )
for idUid in ${UIDs[*]}
do
splittet=( $(echo ${idUid} | tr ':' ' ') )
username=$(niutil -readprop . /users/uid=${splittet[1]} name)
niutil -createprop . /users/name=${username} authentication_authority ";ShadowHash;"
done
;;
* )
for username in ${*}
do
niutil -createprop . /users/name=${username} authentication_authority ";ShadowHash;"
done
;;
esac

ps -auxww -U root \
| grep -E "loginwindow" \
| tr -s ' ' \
| cut -d ' ' -f 2 \
| xargs -n 1 sudo kill -HUP

[14:47:22@bashScripts]$

killuser forcefully logs out a single use and is used this way:
./killuser [username]

activeUser is given a list of the users that are present on any given day:
./activeUser all | [username] ...
BjarneDM - Jun 10, 2006 - 8:00 am
image
image
Of course, the formatting goes bonkers in here :-(((

And these two lines has to be a single one with a [space] between them:
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend

I launch the activeUser script from cron with lines like these in /etc/crontab:
0 5 * * wed root ~root/bashScripts/activeUser sille joy
Mickwilli - Jun 10, 2006 - 10:28 pm
image
image
Hay that sounds great. Does it come in English??

How then would I get this into my system to simply force logoff?
I have found that i can do a:
"Tell Application "loginwindow" to quit" in apple script but that of course does nothing as it has to be forced to quit to do anything useful. Anyone would think that it wouldn't be hard to get it to force quit but apple script doesn't seam to have a force quit just a polite one.

If you could give me some simple instructions (I HATE terminal / unix stuff) so I can get this into a cronjob to run at some time in the night. Weve been using a application called cronix (Or something like that) to shedlue apple script programs.

Mike
Mickwilli - Jun 10, 2006 - 10:35 pm
image
image
Oh BTW the script needs to be able to run without the need to be logged into root or an admin, as it is to be run in a restricted account. The whole idea is that these orginasations computers will logout (Or reboot) at 7:00pm and totally ignore any open applictions (I.e. Microsoft Word open with a request for what you want to do will can a normal logout).

It will then run an applescript to chnage the desktop background and delete the contence or the music folder, the keychain and the actuall desktop itself. Then after awhile it will have a little sleep.
BjarneDM - Jun 11, 2006 - 7:17 am
image
image
you edit the file /etc/crontab
I gave you an example in the last two lines of my reply

"man 1 crontab" and "man 5 crontab" executed in terminal will give you all the needed information. /etc/crontab is the systemwide crontab . All crontab files - whether they are the systemwide one or user specific - will run whether or not any user at all is logged in.

you can also run applescript from crontab files: the "osascript" Terminal command gives you the option of executing applescripts saved as plain text files. "man osascript" gives the necessary information.

my killuser script will forcefully log any user given as a parameter out and forcefully kill any process/program running under that user. My activeUser script starts by forcefully logging any other user than 'bjarne' out. If you've got a litte bit of insight into programming you ought to be able to discern what it does and be able to modify according to your needs :-)
Mickwilli - Jun 12, 2006 - 6:45 pm
image
image
We are using "CronniX" to put in our cron jobs. It's much nicer.

Can you have a look at this Application and tel me how I could get your script into the job and working.

That would be much appreciated!!

Mike

IF THIS IS YOUR QUESTION AND YOU WISH TO RESPOND, LOGIN HERE FIRST.


Search Engine Friendly URLs by vBSEO 3.1.0