有什么方法可以将应用程序/脚本添加到Linux启动中,以便每次系统启动时都将执行它。
我正在寻找某种自动化的方式,即用户不应按cron作业或类似方式添加此内容。
有什么方法可以将应用程序/脚本添加到Linux启动中,以便每次系统启动时都将执行它。
我正在寻找某种自动化的方式,即用户不应按cron作业或类似方式添加此内容。
Answers:
除了系统级启动脚本之外,您的桌面环境还可能具有其自己的自动运行程序的方式。该文件夹.config/autostart应该是定义自动运行项的与桌面无关的方式。/etc/xdg/autostart用于系统范围的配置。有关规范的详细信息,请参见http://developer.gnome.org/autostart-spec/。
对于LXDE,也可以在中设置自动启动条目~/.config/lxsession/LXDE/autostart。
如果在网络启动并运行后需要运行脚本,则有所不同。在这种情况下,您应该检查可以为网络管理员定义的特殊的连接后脚本。这两种网络管理器和WICD有自己指定连接后自动运行项的方式。如果通过来配置网络ifupdown,则可以将发布脚本放置在/etc/network/if-up.d/文件夹中。但是,可能有一种更好的方法来运行连接后脚本(对于支持它的系统,这是现代发行版中的大多数)。
如果您要自动启动的东西不是需要桌面的图形应用程序,那么最好避免使用xorg或当前桌面环境提供的任何自动启动功能。
systemd 在许多现代发行版中已变得无处不在,并且就如何启动服务和如何运行它们提供了很多控制和灵活性。
我将总结一些好处( systemd可以做更多的事情):
User=myuserRestart=on-failure|on-watchdog|on-abnormal|alwaysType=simple|forking|oneshot|notify|dbusWants=network-online.target在本[Unit]节中)。启动telegram-cli守护程序的示例服务。将其放入/etc/systemd/system/tg.service。
[Unit]
Description=MyDaemon
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/usr/local/bin/telegram-cli -k /etc/telegram-cli/tg-server.pub -W -P 1234 -d -vvvRC
ExecStop=/usr/bin/pkill -f telegram-cli
User=jicu
[Install]
WantedBy=multi-user.target
现在,您可以启用该服务以自动启动:
sudo systemctl enable tg
启动服务:
sudo systemctl start tg
停止服务:
sudo systemctl stop tg
检查状态:
systemctl status tg
禁用服务:
sudo systemctl disable tg
为了节省您的额外输入,您可以~/.bashrc在行中添加,alias sc='sudo systemctl $*'然后将上面的命令缩短为例如sc start tg。
注意:如果您曾经使用过,
cron那么请注意crontab条目是在受限环境中运行的—同样适用于systemd:始终使用绝对路径,并且不假设已定义任何变量。明确设置脚本依赖的任何变量。systemd不会使用您用户的.bashrc和$PATH。
更多信息:
是的,可以通过rc.local在/etc或/etc/rc.d目录中定义可执行文件的路径来在Linux上启动时运行程序,例如:
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
/path/to/executable
注意:不要忘记按照文件文档中的说明分配可执行权限,即 Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure that this script will be executed during boot.
Startup applications使用主页按钮Add Startup command box输入命令OK(您应该在列表中看到新命令)Close 通过重新启动或注销然后重新登录进行测试。
来源:https://help.ubuntu.com/community/AddingProgramToSessionStartup
我在这里找到了答案:https : //stackoverflow.com/questions/7221757/run-automatically-program-on-startup-under-linux-ubuntu 我能够创建文件/脚本来关闭触控板我的Linux Ubuntu 12.10会话。