假设需要一个名为install.sh的Bash安装脚本,以确保Ubuntu22.04在引导时运行命令:picard (启动GUI) (在后台,没有显示GUI)。
我查看了:https://askubuntu.com/a/816/846880,它允许通过键入以下命令手动完成此操作:
crontab -e
[1] (to select the favourite editor)
@reboot /path/to/script但是,我希望自动设置cronjob,而不是手动设置。所以我查看了GitHub:并找到了此安装脚本,它包含:
#para crontab -e
#echo "@reboot sudo /home/pi/ili9342-driver/fbcp-ili9342" >> mycron; crontab mycron;rm mycron但是,这并没有考虑到所需的行是否已经在crontab中。
因此,我想知道如何使单个Bash脚本在引导时自动运行命令picard?(理想情况下,在后台不显示GUI)
发布于 2022-06-19 14:38:36
下面的答案还没有在后台运行picard,我也没有为它编写单元测试。但是,它会在从bash脚本重新启动时自动设置crontab。
run_tribler_at_boot() {
local git_dir="$1"
# Create cronjob command
cron_command="@reboot $git_dir/tribler/src/./tribler.sh > tribler.log"
# Check if crantab contains run at boot line already:
found_cron_command=$(crontab -l | grep "$cron_command")
if [ "$found_cron_command" == "" ]; then
# Add cronjob to crontab if it is not in yet.
write_cronjob "$git_dir" "tribler_cron" "$cron_command"
elif [ "$found_cron_command" == "$cron_command" ]; then
echo "cronjob is already in crontab."
else
echo "Error, the cronjob was not correctly in the crontab"
echo "(most likely the cron_command:$cron_command is in in duplo):"
echo ""
crontab -l
echo ""
echo "Remove the (duplicate) cron_command(s) from the crontab -e"
echo "and try again."
exit 6
fi
}
write_cronjob() {
local git_dir="$1"
local temp_cronjob_filename="$2"
local cron_command="$3"
# Write out the current crontab into a new file with filemname in var mycron.
crontab -l > $temp_cronjob_filename
#echo new cron into cron file through append.
echo "$cron_command" >> $temp_cronjob_filename
#install new cron file
crontab $temp_cronjob_filename
# TODO: Verify the temp_cronjob_filecontent equals the crontab -e output.
#rm $temp_cronjob_filename
}它可以被称为:
run_tribler_at_boot() "~/git"假设应用程序(在本例中为tribler )存储在~/git/tribler/src/tribler.sh中以更改为picard,则可以将cron_command设置为:
cron_command="@reboot picard"我还不知道如何在后台运行它。
https://askubuntu.com/questions/1413910
复制相似问题