Đây là trình khởi chạy Python, dựa trên câu trả lời này và một nghiên cứu bổ sung trên Internet, hoạt động tốt trong Ubuntu 16.04:
#!/usr/bin/env python3
import signal
import gi
import os
import subprocess
import sys
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, AppIndicator3, GObject
import time
from threading import Thread
# Execute the script
script = os.path.basename(sys.argv[1])
subprocess.Popen(sys.argv[1:])
script_name = script.rsplit('/', 1)[-1]
class Indicator():
def __init__(self):
self.app = 'Script indicator'
iconpath = "/usr/share/unity/icons/launcher_bfb.png"
self.indicator = AppIndicator3.Indicator.new(
self.app, iconpath,
AppIndicator3.IndicatorCategory.OTHER)
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
self.indicator.set_menu(self.create_menu())
self.indicator.set_label("Script Indicator", self.app)
# the thread:
self.update = Thread(target=self.show_seconds)
# daemonize the thread to make the indicator stopable
self.update.setDaemon(True)
self.update.start()
def create_menu(self):
menu = Gtk.Menu()
# menu item 1
item_quit = Gtk.MenuItem('Quit')
item_quit.connect('activate', self.stop)
menu.append(item_quit)
menu.show_all()
return menu
def show_seconds(self):
global script_name
t = 0
process = subprocess.call(['pgrep', script_name], stdout=subprocess.PIPE)
while (process == 0):
t += 1
GObject.idle_add(
self.indicator.set_label,
script_name + ' ' + str(t) + 's', self.app,
priority=GObject.PRIORITY_DEFAULT
)
time.sleep(1)
process = subprocess.call(['pgrep', script_name], stdout=subprocess.PIPE)
subprocess.call(['notify-send', script_name + ' ended in ' + str(t) + 's'])
time.sleep(10)
Gtk.main_quit()
def stop(self, source):
global script_name
subprocess.call(['pkill', script_name], stdout=subprocess.PIPE)
Gtk.main_quit()
Indicator()
# this is where we call GObject.threads_init()
GObject.threads_init()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()
- Nếu bạn thấy bất kỳ cách nào để cải thiện tập lệnh, xin vui lòng, đừng ngần ngại chỉnh sửa câu trả lời. Tôi chưa có nhiều kinh nghiệm với Python.
Tạo tập tin thực thi và đặt các dòng trên làm nội dung của nó. Giả sử tập tin được gọi script-indicator.py
. Tùy thuộc vào nhu cầu của bạn và bản chất tập lệnh của bạn, bạn có thể sử dụng trình khởi chạy này theo một trong các cách sau:
./script-indicator.py /path/to/script.sh
./script-indicator.py /path/to/script.sh &
./script-indicator.py /path/to/script.sh > out.log &
./script-indicator.py /path/to/script.sh > /dev/null &
- Đâu
script.sh
là một trong những bạn muốn chỉ ra.
Ảnh chụp màn hình được thực hiện ngay khi script.sh
kết thúc:

- Click vào hình ảnh để xem một bản demo hoạt hình.
Ngoài ra, bạn có thể đặt tập lệnh vào /usr/local/bin
để có thể truy cập dưới dạng hệ thống lệnh shell. Bạn có thể tải xuống từ GitHub Gist dành riêng này :
sudo wget -qO /usr/local/bin/script-indicator https://gist.githubusercontent.com/pa4080/4e498881035e2b5062278b8c52252dc1/raw/c828e1becc8fdf49bf9237c32b6524b016948fe8/script-indicator.py
sudo chmod +x /usr/local/bin/script-indicator
Tôi đã thử nó với cú pháp sau:
script-indicator /path/to/script.sh
script-indicator /path/to/script.sh &
script-indicator /path/to/script.sh > output.log
script-indicator /path/to/script.sh > output.log &
script-indicator /path/to/script.sh > /dev/null
script-indicator /path/to/script.sh > /dev/null &
nohup script-indicator /path/to/script.sh >/dev/null 2>&1 &
# etc...