串口自动登录实现程序自启动

本文详细介绍了如何通过脚本设置Ubuntu系统自动登录指定用户,并在登录后自动运行程序。首先创建一个autologin脚本,然后修改systemd服务的serial-getty@.service文件,选择合适的方式实现自动登录。接着,在用户的.profile文件中添加程序启动命令,确保只在串口登录时启动程序。最后,通过systemctl启用服务并重启验证设置是否成功。

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

本文实现通过串口自动登录Ubuntu系统,并自动运行程序

1. 自动登录

1. 写脚本autologin

#!/bin/bash
/bin/login -f username #你的用户名

保存到/usr/bin/下,并且用`chmod +x autologin`设置可执行权限

2. 配置systemd服务
修改系统中/lib/systemd/system/serial-getty\@.service 文件内容,修改ExecStart项,实现自动启动有两种方式:

  • ExecStart=-/sbin/agetty -a root 115200 %I $TERM 其中root可以替换为任何用户,这种方式实现自动登录。
  • ExecStart=-/sbin/agetty  -n -l /usr/bin/autologin 115200 %I $TERM` 这种方式通过执行autologin脚本来实现登录。
$ sudo vim /lib/systemd/system/serial-getty\@.service
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Serial Getty on %I
Documentation=man:agetty(8) man:systemd-getty-generator(8)
Documentation=http://0pointer.de/blog/projects/serial-console.html
BindsTo=dev-%i.device
After=dev-%i.device systemd-user-sessions.service plymouth-quit-wait.service getty-pre.target
After=rc-local.service

# If additional gettys are spawned during boot then we should make
# sure that this is synchronized before getty.target, even though
# getty.target didn't actually pull it in.
Before=getty.target
IgnoreOnIsolate=yes

# IgnoreOnIsolate causes issues with sulogin, if someone isolates
# rescue.target or starts rescue.service from multi-user.target or
# graphical.target.
Conflicts=rescue.service
Before=rescue.service

[Service]
# The '-o' option value tells agetty to replace 'login' arguments with an
# option to preserve environment (-p), followed by '--' for safety, and then
# the entered username.
#ExecStart=-/sbin/agetty -o '-p -- \\u' --keep-baud 115200,38400,9600 %I $TERM
#ExecStart=-/sbin/agetty -a root 115200 %I $TERM
ExecStart=-/sbin/agetty  -n -l /usr/bin/autologin 115200 %I $TERM
Type=idle
Restart=always
UtmpIdentifier=%I
TTYPath=/dev/%I
TTYReset=yes
TTYVHangup=yes
KillMode=process
IgnoreSIGPIPE=no
SendSIGHUP=yes

[Install]
WantedBy=getty.target

配置服务器自动启动

$ sudo systemctl enable getty@tty1.service
Created symlink /etc/systemd/system/getty.target.wants/getty@tty1.service → /lib/systemd/system/getty@.service.

重启验证服务

Last login: Fri Aug 13 00:33:41 UTC 2021 from 192.168.31.150 on pts/2
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.19.95+ aarch64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

 * Super-optimized for small spaces - read how we shrank the memory
   footprint of MicroK8s to make it the smallest full K8s around.

   https://ubuntu.com/blog/microk8s-memory-optimisation
HwHiAiUser@davinci-mini:~$

2. 程序自启动


通过在登录用户home目录下的.profile脚本添加程序启动命令来启动程序。

修改.profile,在文件尾部添加以下内容

 #注意此处加判断是防止程序重复启动,由于.profile中的内容,在每次登录都会执行,如果用户通过ssh等其他方式登录会导致程序重复启动。
    # $DISPLAY是可选项,如果是GUI的程序建议加上
    if [ -z "$DISPLAY" ] && [ $(tty) == /dev/tty1 ]; then
         #此处添加你的启动程序的命令
         echo "run your application here!"
    fi

3. 重启,确实是不是自动登陆并执行了?

Last login: Fri Aug 13 00:33:41 UTC 2021 from 192.168.31.150 on pts/2
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.19.95+ aarch64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

 * Super-optimized for small spaces - read how we shrank the memory
   footprint of MicroK8s to make it the smallest full K8s around.

   https://ubuntu.com/blog/microk8s-memory-optimisation
run your application here!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值