bash 如果正在运行,则重新启动 dropbox-daemon

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15455324/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:53:32  来源:igfitidea点击:

Restarting dropbox-daemon if running

linuxbashubuntu-12.04dropbox

提问by hild

I have a couple of encrypted drives that I mount manually with a script after start. One of those drives hosts my dropbox folder.

我有几个加密驱动器,在启动后我用脚本手动安装。其中一个驱动器托管我的 Dropbox 文件夹。

I need to check if dropbox is running when mounting the drive so that I can stop dropbox and then start it again so it syncs correctly.

我需要在安装驱动器时检查 dropbox 是否正在运行,以便我可以停止 dropbox 然后再次启动它,以便它正确同步。

This is what I have so far, but I can't get it to stop dropbox if it's already running.

这是我到目前为止所拥有的,但如果它已经在运行,我无法让它停止 dropbox。

#!/bash/rc
if ~/dropbox.py running && [ $? -eq 1 ]; then
    ~/dropbox.py stop
else
    ~/dropbox.py start
fi

回答by Aleks-Daniel Jakimenko-A.

Try

尝试

killall dropbox

This is going to stop it for sure! By default SIGTERM is sent, which is a right way to stop a process. If your system supports multiple simultaneous user logins, then this command is going to terminate dropbox for all users, or at least it will attempt to do so. So more elegant way is to use

这肯定会阻止它!默认情况下发送 SIGTERM,这是停止进程的正确方法。如果您的系统支持多个用户同时登录,那么此命令将终止所有用户的 dropbox,或者至少它会尝试这样做。所以更优雅的方式是使用

killall -u myusername dropbox

and if you're currently logged in by that user:

如果您当前是由该用户登录的:

killall -u "$(whoami)" dropbox

Or maybe even

或者甚至

killall -u "$USER" dropbox

Update:well, it seems like people like this answer. However, there is one important thing to know. Just sending a signal to a process does not really mean that it will terminate immediately (or that killall is going to wait for it to terminate). So it is possible that dropbox will attempt to terminate safely (which could take some time to finish) when you assume that it is already gone. Just a thing to consider.

更新:好吧,似乎人们喜欢这个答案。然而,有一件重要的事情要知道。仅仅向进程发送信号并不意味着它会立即终止(或者 killall 将等待它终止)。因此,当您认为 Dropbox 已经消失时,它可能会尝试安全终止(这可能需要一些时间才能完成)。只是一个需要考虑的事情。