简体   繁体   English

关闭Mac的脚本

[英]Script to shutdown mac

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. 我正在尝试自动关闭Mac,我尝试了节能中的计划关闭,但我想睡觉,但是这些似乎没有用。 VLC player runnign seems to prevent the shutdown. VLC播放器的运行似乎阻止了关机。 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,无论运行的各种程序可能在屏幕上看到什么错误。

Thanks 谢谢

Ok, 好,

This is the applescript code im using to shutdown may mac. 这是用于关闭可能的mac的applescript代码。 I've added it as an iCal event thats runs nightly. 我已将其添加为每晚运行的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

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运行,等等。

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 ;-) ) 始终为我工作以确保关机(爆震;-))

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 . 使用sudokill时,无论哪种方式,您都在玩火。

do what linux users do . 做linux用户的事情 use a bash script. 使用bash脚本。 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: 如果您不知道如何创建一个,请继续下载并使用互联网搜索找到任何bash脚本,然后使用文本编辑应用程序将其打开并粘贴以下内容:
( 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 ) (请注意,如果许多人使用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. 它的工作方式与在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. 如果我们选择例如50,则PC(niresh)或mac将在50分钟内关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM