有没有办法强制用户在Ubuntu(16.04或18.04)中使用C ++注销?就像条件满足一样,我希望程序注销当前用户。
在Windows 10中,我们可以使用像ExitWindows
这样的https://docs.microsoft.com/en-us/windows/desktop/shutdown/how-to-log-off-the-current-user。
在Ubuntu有可能吗?我找不到一个很好的例子如何做到这一点。
这是特定于窗口管理器的,因此最简单的方法是使用exec
函数来完成它。 Ubuntu 18.04默认使用Gnome,所以在Gnome中你会做以下事情:
#include <unistd.h>
#include <stdlib.h>
int main()
{
if (execl("/usr/bin/gnome-session-quit", "/usr/bin/gnome-session-quit",
"--no-prompt", (char*) NULL) < 0)
printf("Failed to logout\n");
}
我不确定loginctl
程序在KDE的位置,所以我假设它在同一个位置,所以对于KDE你会:
#include <stdlib.h>
...
char *user=getenv("USER");
if (execl("/usr/bin/loginctl", "/usr/bin/loginctl",
user, (char*) NULL) < 0)
printf("Failed to logout\n");
您可以使用system()
中的c ++ stdlib.h
调用任何操作系统命令。
#include<stdlib.h>
int main(){
system("gnome-session-quit"); //logs out.
}
据我所知,上述代码在ubuntu中执行后,如果有任何未保存的工作,它会在60秒后自动注销。