Linux debian 10如何使脚本在启动时运行程序,并自动将值输入到该程序控制台



大家好,我正在自学linux,我用python编写了一个程序示例,文件名为"sample_ login";在"/home/user/Desktop/sample_login";

ID = Input("ID: ")
print (ID)
pw = Input("PW: ")
print (pw)

现在我想制作一个shell脚本,它可以更改到这个程序目录并运行我的程序,并在重新启动后自动将id和pw输入到程序控制台,那么我该怎么做呢?我的意思是按照执行命令

cd /home/user/Desktop
./sample_login
#wait ID: 
#input user_id
...

非常感谢你们

为了在启动时运行脚本,可以使用systemd服务文件。

sample.service:

[Unit]
Description=sample service
[Service]
User=johndoe
WorkingDirectory=/path/to/working_dir/
ExecStart=/path/to/working_dir/your_script
Restart=always
[Install]
WantedBy=multi-user.target

您只需要将此文件移动到/etc/systemd/system/.并使用systemctl enable sample.service启用它,这样它就可以在启动时启动。您可以接受在自动化脚本中更容易处理的命令行参数,而不是期望输入。

最新更新