簡體   English   中英

Linux debian 10 如何使腳本在啟動時運行程序並自動向該程序控制台輸入值

[英]Linux debian 10 how to make a script run a program at startup and auto input value to this program console

大家好,我正在為自己學習 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服務文件。

示例服務:

[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啟用它,以便它在啟動時啟動。 您可以接受在自動化腳本中更容易處理的命令行參數,而不是期待輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM