簡體   English   中英

Ubuntu - 使用可見終端在啟動時運行 bash 腳本

[英]Ubuntu - run bash script on startup with visible terminal

我想在 Ubuntu 20.04 啟動時運行 bash 腳本,終端可見 test.sh 文件位於 /usr/bin/test.sh。 我可以讓 test.sh 文件在啟動時運行,但不能在可見終端 window 中運行。

test.sh 的內容:

#! /bin/bash
echo "hello";

我無法讓它工作,我已經嘗試過(個人):

Crontab(帶和不帶 '&' 和帶/不帶“sudo”)

@reboot bash test.sh &

@reboot /usr/bin/test.sh &

@reboot DISPLAY=:0 xterm -hold -e bash -c "bash test.sh" &

@reboot DISPLAY=:0 xterm -hold -e bash -c "bash /usr/bin/test.sh" &

啟動應用程序命令

sudo bash /usr/bin/test.sh
bash /usr/bin/test.sh
/usr/bin/test.sh

在 /etc/systemd/system/testService.service 創建服務

[Unit]
Description = Test Service

[Service]
WorkingDirectory= /usr/bin
ExecStart= /usr/bin/test.sh

[Install]
WantedBy=multi-user.target

並啟動,啟用和檢查狀態..

systemctl start testService.service
systemctl enable testService.service
systemctl status testService.service

但未能啟動。

任何幫助/指向更好的方向將不勝感激!

當您啟動 Unix 時,X 服務器將在啟動結束時啟動。 只有當有人登錄時,啟動 X 客戶端才有意義。所以你的目標“在計算機啟動時啟動 X 客戶端”是沒有意義的,因為當你嘗試啟動 X 客戶端時沒有運行 X 服務器。

您可以在登錄后啟動 X 客戶端。 如果您使用經典安裝,請為此使用.xinitrc 如果您使用不同的桌面環境,請使用此桌面環境為您提供的任何內容。

要在運行腳本時顯示 GUI 終端 window:

添加到“啟動應用程序”(在命令下):

bash test.sh

test.sh 的內容:

#! /bin/bash
    DISPLAY=:0.0 xterm -hold -e bash helloWorld.sh

helloWorld.sh 的內容:

#! /bin/bash
echo "hello";

對我來說,這在登錄時打開了一個 XTerm 終端 window 並運行 helloWorld.sh 腳本。

暫無
暫無

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

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