Ubuntu 自动运行用户脚本的方法

本文介绍了在Ubuntu 16.04系统中如何设置脚本随开机或桌面启动。对于不涉及桌面图像界面的脚本,可以修改 /etc/rc.local 文件;涉及桌面的脚本,可以通过图形界面或命令行配置,如gnome-session-properties和创建.desktop文件。如果遇到权限问题,可创建密码文件或修改用户权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux 系统有两种交互模式,文本交互及图形化桌面交互,根据用户的脚本功能的不同,就要选择合适的启动方式。

下面介绍两种随开机自启动脚本方法。

如果脚本功能不涉及桌面图像界面的话,就使用方法一,否则使用方法二。

方法一:

修改 /etc/rc.local 文件,该文件是Linux系统运行级别为2-5时启动调用的文件,可在该文件中添加运行脚本的指令。

如运行 /opt/mystart.sh 脚本,添加如下,保存,即可随系统启动自动运行脚本。

# vim /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Generate the SSH keys if non-existent
if [ ! -f /etc/ssh/ssh_host_rsa_key ]
then
# else ssh service start in dpkg-reconfigure will fail
systemctl stop ssh.socket||true
# dpkg-reconfigure openssh-server
dpkg --force-confdef --force-confold --configure -a
fi

/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值