我对脚本非常非常陌生。这是我编写的简单脚本(有效),它在 5 分钟后显示图像:
sleep 300 && firefox file:///home/tasks/fiveminutes.jpg
Run Code Online (Sandbox Code Playgroud)
这是我的问题:有时我不记得我是否启动了计时器。有没有办法添加显示 5 分钟(从我启动计时器到我结束计时器的那一刻)的文本或图标指示(理想情况下在任务栏上,但任何地方都很棒)?
非常感谢您的任何建议。
拥有这样的脚本就足够了:
#!/usr/bin/env bash
while true
do
if pgrep -f gedit > /dev/null
then
printf "\r%b" "\033[2K"
printf "Script is running"
else
printf "\r%b" "\033[2K"
printf "Script is not running."
fi
sleep 0.25
done
Run Code Online (Sandbox Code Playgroud)
这是一种非常典型的方式,常用于 shell 脚本中。它的作用是连续运行,并且每四分之一秒检查您的脚本是否通过pgrep -f. if..fishell 脚本中的语句对命令的退出状态进行操作,因此pgrep -f返回成功的退出状态意味着它找到了脚本的进程并且正在运行。否则,我们转到 else 语句。
在这两种情况下,我们都会打印一行文本告诉用户它是否正在运行。还添加了printf "\r%b" "\033[2K"用于打印用于清除行的转义序列(仅用于更清晰的输出)。
从那里,天空是极限。您可以通过管道将该文本传递给终端中的另一个进程,或者您可以将监控脚本的输出传递给indicator-sysmonitor,它允许在指标中显示自定义信息。它可以通过安装
sudo add-apt-repository ppa:fossfreedom/indicator-sysmonitor
sudo apt-get update
sudo apt-get install indicator-sysmonitor
Run Code Online (Sandbox Code Playgroud)
之后,只需将指标配置为显示自定义命令,即监控脚本。可以在此处找到配置自定义命令的示例。
当然,人们可以用 Python 编写他们自己的指标,但是当已经有一个工具时,这可能只是一种矫枉过正的工具。
这是基于此答案和 Internet 上的其他研究的Python 启动器,它在 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()
Run Code Online (Sandbox Code Playgroud)
创建可执行文件并将上述行作为其内容。假设文件名为script-indicator.py. 根据您的需要和脚本性质,您可以通过以下方式之一使用此启动器:
./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 &
Run Code Online (Sandbox Code Playgroud)
script.sh你想指出的那个在哪里。刚刚script.sh结束时的截图:
或者,您可以将脚本放置在/usr/local/bin可作为 shell 命令系统范围访问的地方。你可以从这个专用的GitHub Gist下载它:
#!/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()
Run Code Online (Sandbox Code Playgroud)
我已经使用以下语法对其进行了测试:
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...
Run Code Online (Sandbox Code Playgroud)
\n\n
osd_cat您可以使用osd_cat来自xosd-bin 包,例如:
<<<"runs" osd_cat -d5 -i20 -o50 -f"-*-*-*-*-*-*-100-*-*-*-*-*-*-*" && firefox\nRun Code Online (Sandbox Code Playgroud)\n\n这会在屏幕上的位置显示 \xe2\x80\x9cruns\xe2\x80\x9d 的字体大小100几秒钟,并在 \xe2\x80\x99s 准备好 \xe2\x80\x93 你不需要\xe2\x80\x99t 时启动用这种方法。您可以使用该选项获取 X 逻辑字体描述符(这个奇怪的东西) ,例如,如果您想使用不同的字体。阅读更多选项。520,50firefoxsleepxfontsel-*-*-\xe2\x80\xa6-fman osd_cat
yad你可以使用yad 如下:
yad --title=runs --text="it\xe2\x80\x99s running!" --timeout=5 --button=Fire:1 --button=Abort:0 || firefox\nRun Code Online (Sandbox Code Playgroud)\n\n这样做的优点是您可以中止命令或立即执行它,如果您不执行任何操作,它将5在本例中几秒钟后关闭。
您可以在进程启动时显示弹出通知。只需将您的脚本更改为
notify-send "Your image will arrive" "after 5 minutes" && sleep 300 && firefox file:///home/tasks/fiveminutes.jpg
Run Code Online (Sandbox Code Playgroud)
-t您可以使用命令的参数(以及以毫秒为单位的时间)设置通知气泡的持续时间notify-send,例如设置 5 分钟的持续时间
notify-send -t 300000 "heading" "text"
Run Code Online (Sandbox Code Playgroud)
但根据您的桌面环境,此参数可能会被完全忽略(请参阅此)。