ubuntu 守护进程?ubuntu桌面环境哪个好

大家好,关于ubuntu 守护进程很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于ubuntu桌面环境哪个好的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决您的问题,还望关注下本站哦,希望对各位有所帮助!

Ubuntu Linux设置守护进程时出错的解决

Ubuntu系统使用源从7.10升级到8.04后,启动并在用户登录后,界面无响应5分钟左右后,弹出出错信息:

启动GNOME设置守护进程时出错

主题、声音或者背景设置等可能不会正常工作。

最后的错误信息是:

Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.

GNOME在您下次登录时仍将试图重启动设置守护进程

解决方法:

在启动至将要登陆到用户桌面时,按Ctrl+ Alt+ F1进入命令行模式,作如下修改:

sudo vim/etc/network/interfaces

auto lo

iface lo inet loopback

auto eth0

iface eth0 inet dhcp

注意“eth0”最后一个字为零,而非英文字母“欧”,完成键盘输入后,按ESC,输入:

wq!

保存并退出文件的修改,使用如下命令重新启动网络服务:

sudo/etc/init.d/networking restart

或者:

sudo ifdown eth0

或者:

sudo ifup eth0

重启系统,发现问题已经解决,锐捷要求的动态ip也可以获取到,可顺利上线了。

如何在Ubuntu中管理开机启动应用

在 Ubuntu中管理开机启动应用

默认情况下, Ubuntu提供了一个Startup Applications工具来供你使用,你不必再进行安装。只需到 Unity面板中就可以查找到该工具。

在 ubuntu中的开机启动应用工具

点击它来启动。下面是我的Startup Applications的样子:

在 Ubuntu中查看开机启动程序

在 Ubuntu中移除开机启动应用

现在由你来发现哪个程序对你用处不大,对我来说,是 Caribou这个软件,它是一个屏幕键盘程序,在开机时它并没有什么用处,所以我想将它移除出开机启动程序的列表中。

你可以选择阻止某个程序在开机时启动,而在开机启动程序列表中保留该选项以便以后再进行激活。点击关闭按钮来保留你的偏好设置。

在 Ubuntu中移除开机启动程序

要将一个程序从开机启动程序列表中移除,选择对应的选项然后从窗口右边的面板中点击移除按钮来保留你的偏好设置。

在 Ubuntu中将程序从开机启动列表中移除

需要提醒的是,这并不会将该程序卸载掉,只是让该程序不再在每次开机时自动启动。你可以对所有你不喜欢的程序做类似的处理。

让开机启动程序延迟启动

若你并不想在开机启动列表中移除掉程序,但同时又忧虑着系统性能的问题,那么你所需要做的是给程序添加一个延迟启动命令,这样所有的程序就不会在开机时同时启动。

选择一个程序然后点击编辑按钮。

编辑开机启动应用列表

这将展示出运行这个特定的程序所需的命令。

在开机启动列表的程序运行所需的命令

所有你需要做的就是在程序运行命令前添加一句 sleep XX;。这样就为实际运行该命令来启动的对应程序添加了 XX秒的延迟。例如,假如我想让 Variety壁纸管理应用延迟启动 2分钟,我就需要像下面那样在命令前添加 sleep 120;

在 Ubuntu中延迟开机启动的程序

保存并关闭设置。你将在下一次启动时看到效果。

增添一个程序到开机启动应用列表中

这对于新手来说需要一点技巧。我们知道,在 Linux的底层都是一些命令,在上一节我们看到这些开机启动程序只是在每次开机时运行一些命令。假如你想在开机启动列表中添加一个新的程序,你需要知道运行该应用所需的命令。

第 1步:如何查找运行一个程序所需的命令?

首先来到 Unity Dash面板然后搜索 Main Menu:

Ubuntu下的程序菜单

这将展示出在各种类别下你安装的所有程序。在 Ubuntu的低版本中,你将看到一个相似的菜单,通过它来选择并运行应用。

Ubuntu下的 main menu

在各种类别下找到你找寻的应用,然后点击属性按钮来查看运行该应用所需的命令。例如,我想在开机时运行 Transmission Torrent客户端。

在 Ubuntu下查找运行程序所需的命令

这就会向我给出运行 Transmission应用的命令:

在 Ubuntu下查找运行某个程序所需的命令

接着,我将用相同的信息来将 Transmission应用添加到开机启动列表中。

第 2步:添加一个程序到开机启动列表中

再次来到开机启动应用工具中并点击添加按钮。这将让你输入一个应用的名称,对应的命令和相关的描述。其中命令最为重要,你可以使用任何你想用的名称和描述。使用上一步得到的命令然后点击添加按钮。

在 Ubuntu中添加一个开机启动程序

就这样,你将在下一次开机时看到这个程序会自动运行。这就是在 Ubuntu中你能做的关于开机启动应用的所有事情。

到现在为止,我们已经讨论在开机时可见到的应用,但仍有更多的服务,守护进程和程序并不在开机启动应用工具中可见。下一节中,我们将看到如何在 Ubuntu中查看这些隐藏的开机启动程序。

在 Ubuntu中查看隐藏的开机启动程序

要查看在开机时哪些服务在运行,可以打开一个终端并使用下面的命令:

sudo sed-i's/NoDisplay=true/NoDisplay=false/g'/etc/xdg/autostart/*.desktop

上面的命令是一个快速查找和替换命令,它将在所有自动启动的程序里的 NoDisplay=false改为 NoDisplay=true,一旦执行了这个命令后,再次打开开机启动应用工具,现在你应该可以看到更多的程序:

在 Ubuntu中查看隐藏的开机启动程序

你可以像先前我们讨论的那样管理这些开机启动应用。我希望这篇教程可以帮助你在 Ubuntu中控制开机启动程序

ubuntu python怎么作为守护进程一直运行

测试程序

先写一个测试程序,用于输出日志和打印到控制台。

#-*- coding: utf-8-*-

import logging

import time

from logging.handlers import RotatingFileHandler

def func():

init_log()

while True:

print"output to the console"

logging.debug("output the debug log")

logging.info("output the info log")

time.sleep(3);

def init_log():

logging.getLogger().setLevel(logging.DEBUG)

console= logging.StreamHandler()

console.setLevel(logging.DEBUG)

formatter= logging.Formatter('%(asctime)s%(filename)s[line:%(lineno)d]%(levelname)s%(message)s')

console.setFormatter(formatter)

logging.getLogger().addHandler(console)

# add log ratate

Rthandler= RotatingFileHandler("backend_run.log", maxBytes=10* 1024* 1024, backupCount=100,

encoding="gbk")

Rthandler.setLevel(logging.INFO)

# formatter= logging.Formatter('%(asctime)s%(filename)s[line:%(lineno)d]%(levelname)s%(message)s')

Rthandler.setFormatter(formatter)

logging.getLogger().addHandler(Rthandler)

if __name__=='__main__':

func()

后台启动Python脚本

可以使用下面的命令来启动上面的脚本,让Python在后台运行。

nohup python-u main.py> test.out 2>&1&

来解释一下这几个命令的参数。这一段来自

其中 0、1、2分别代表如下含义:

0– stdin(standard input)

1– stdout(standard output)

2– stderr(standard error)

nohup python-u main.py> test.out 2>&1&

nohup+最后面的&是让命令在后台执行

>out.log是将信息输出到out.log日志中

2>&1是将标准错误信息转变成标准输出,这样就可以将错误信息输出到out.log日志里面来。

运行命令后,会返回一个pid。像下面这样:

[1] 9208

后续可以学习Hadoop它们,把pid存起来,到时候stop的时候就把它杀掉。

跟踪输出文件变化

为了验证脚本可以在后台继续运行,我们退出当前会话。然后重新连接一个Session,然后输入下面的命令来跟踪文件的输出:

tail-f test.out

输出内容如下:

output to the console

2017-03-21 20:15:02,632 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:02,632 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:05,635 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:05,636 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:08,637 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:08,638 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:11,640 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:11,642 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:14,647 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:14,647 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:17,653 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:17,654 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:20,655 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:20,656 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:23,661 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:23,661 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:26,665 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:26,666 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:29,670 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:29,671 main.py[line:12] INFO output the info log

说明我们的脚本确实在后台持续运行。

结束程序

可以直接通过之前的那个pid杀掉脚本,或者可以通过下面的命令查找pid。

ps-ef| grep python

输出的内容如下:

root 1659 1 0 17:40? 00:00:00/usr/bin/python/usr/lib/python2.6/site-packages/ambari_agent/AmbariAgent.py start

root 1921 1659 0 17:40? 00:00:48/usr/bin/python/usr/lib/python2.6/site-packages/ambari_agent/main.py start

user 8866 8187 0 20:03? 00:00:06/usr/bin/python3/usr/bin/update-manager--no-update--no-focus-on-map

root 9208 8132 0 20:12 pts/16 00:00:00 python-u main.py

root 9358 8132 0 20:17 pts/16 00:00:00 grep--color=auto python

可以看到我们的pid是9208,调用kill杀掉就可以了。

kill-9 9208

编写启动及停止脚本

启动脚本

#!/bin/sh

pid=`ps-ef|grep"python-u main.py"| grep-v"grep"|awk'{print$2}'`

if ["$pid"!="" ]

then

echo"main.py already run, stop it first"

kill-9${pid}

fi

echo"starting now..."

nohup python-u main.py> test.out 2>&1&

pid=`ps-ef|grep"python-u main.py"| grep-v"grep"|awk'{print$2}'`

echo${pid}> pid.out

echo"main.py started at pid:"${pid}

停止脚本

#!/bin/sh

pid=`ps-ef|grep"python-u main.py"| grep-v"grep"|awk'{print$2}'`

if ["$pid"!="" ]

then

kill-9${pid}

echo"stop main.py complete"

else

echo"main.py is not run, there's no need to stop it"

fi

稍后我会把实例代码上传到资料共享里面。

阅读剩余
THE END