macos 关闭mac的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5565659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Script to shutdown mac
提问by blue-sky
I'm trying to automate the shutdown of my mac, I've tried the scheduled shutdown in energy saver and I wanna sleep but these don;t seem to work. VLC player runnign seems to prevent the shutdown. I think I need a script to forcefully shutdown the mac regardless of of what errors may thrown to screen by various programs running.
我正在尝试自动关闭我的 mac,我试过在节能器中按计划关闭,我想睡觉,但这些似乎不起作用。VLC 播放器 runnign 似乎可以防止关机。我想我需要一个脚本来强行关闭 mac,不管运行的各种程序可能会向屏幕抛出什么错误。
Thanks
谢谢
Ok,
行,
This is the applescript code im using to shutdown may mac. I've added it as an iCal event thats runs nightly.
这是我用来关闭可能mac的applescript代码。我已将其添加为每晚运行的 iCal 事件。
tell application "System Events" to set the visible of every process to true
set white_list to {"Finder"}
try
tell application "Finder"
set process_list to the name of every process whose visible is true
end tell
repeat with i from 1 to (number of items in process_list)
set this_process to item i of the process_list
if this_process is not in white_list then
do shell script "killall \"" & this_process & "\""
end if
end repeat
on error
tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try
tell application "System Events"
shut down
end tell
回答by notthetup
Could you try a simple applescript, which goes something like this...
你能试试一个简单的applescript,它是这样的......
tell application "System Events"
shut down
end tell
See if it works, and then you can make it run through Automator at certain time, etc.
看看它是否有效,然后你可以让它在某个时间通过Automator运行,等等。
回答by job
my solution (somwhat late). Just a bash script with apple in it:
我的解决方案(有点晚了)。只是一个带有苹果的 bash 脚本:
#!/bin/bash
# OK, just shutdown all ... applications after n minutes
sudo shutdown -h +2 &
# Try normal shutdown in the meantime
osascript -e 'tell application "System Events" to shut down'
I also edited the /etc/sudoers (and /private/etc/sudoers) file(s) and added the line: ALL=NOPASSWD: /sbin/shutdown
我还编辑了 /etc/sudoers(和 /private/etc/sudoers)文件并添加了以下行:ALL=NOPASSWD: /sbin/shutdown
Always worked for me for an assured shutdown (knock knock ;-) )
一直对我来说有效以确保关机(敲门声;-))
回答by Regexident
This should do:
这应该做:
do shell script "shutdown" with administrator privileges
If you want to pass the admin password from key chain, with no prompt:
如果你想从钥匙串传递管理员密码,没有提示:
do shell script "shutdown" with administrator privileges password "password here"
But do not store the admin password in clear anywhere. Instead use the keychain access.
但不要将管理员密码存储在任何地方。而是使用钥匙串访问。
Alternatively you could kill all user processes, via:
或者,您可以通过以下方式杀死所有用户进程:
do shell script "kill -9 -1"
This however would also kill your own Applescript process, preventing it from requesting the shutdown/restart afterwards.
但是,这也会杀死您自己的 Applescript 进程,从而阻止它之后请求关闭/重新启动。
Either way you're playing with fire, when using sudo
or kill
.
无论哪种方式,当使用sudo
或时,您都是在玩火kill
。
回答by Pavlos Theodorou
do what linux users do. use a bash script. if u dont know how to create one just go ahead and download ANY bash script u find using your internet search and open it with text edit app and paste the following:
( be careful if many people use the pc , then this method is not recommended, cause they can learn your user login password from inside this script )
做 linux 用户做的事情。使用 bash 脚本。如果您不知道如何创建一个,请继续下载您使用 Internet 搜索找到的任何 bash 脚本,然后使用文本编辑应用程序打开它并粘贴以下内容:(
如果很多人使用 PC,请注意,不推荐使用此方法) ,因为他们可以从这个脚本中了解您的用户登录密码)
#!/bin/bash
echo -n "Enter a number > "
read x
echo [your password] | sudo -S shutdown -h +$x
it will work the same way it works in linux. the terminal will pop up a message and ask you to enter a number. if we choose for exaple 50 , then the pc ( niresh ) or mac will shutdown in 50 minutes.
它的工作方式与在 linux 中的工作方式相同。终端会弹出一条消息,要求你输入一个数字。如果我们选择 exaple 50 ,那么 pc ( niresh ) 或 mac 将在 50 分钟内关闭。