简体   繁体   English

如何从 Ubuntu 中的启动终端执行脚本正确启动脚本?

[英]How to start a script properly from a starter terminal executed script in Ubuntu?

Assume you have a starter in your panel which starts a script called foo.sh in terminal mode.假设您的面板中有一个启动器,它在终端模式下启动一个名为 foo.sh 的脚本。

foo.sh than starts another script called bar.sh in background. foo.sh 在后台启动另一个名为 bar.sh 的脚本。

I run into problems:我遇到问题:

First: the name of the written file from bar.sh differs.第一:从 bar.sh 写入的文件名不同。

Second and main: the bar.sh script is not independent and die when the terminal is closed.第二个也是主要的:bar.sh 脚本不是独立的,并且在终端关闭时死掉。

I tested this on an old 16.04.我在旧的 16.04 上对此进行了测试。 machine.机器。

foo.sh foo.sh

#!/bin/bash

#read an user input
read -r -t 60 -p ":" foo
echo "$foo"

#call bar script
/home/$USER/bar.sh "10" "$foo" & disown

#show if bar.sh was started
echo $?

#sleep a short time to see the echo's
sleep 3

bar.sh酒吧.sh

#!/bin/bash

#sleep some time
[[ -n "$1" ]] && sleep $1

#then write user input to file called output
echo "$2 - sleept $1 seconds" >> /home/$USER/output

If the time to sleep in foo is greather than in bar the output file is written but called output?如果 foo 中的睡眠时间大于 bar 中的睡眠时间,则写入 output 文件但称为 output? (with an? at the end). (最后有一个?)。

But if the time is larger in bar then bar is killed when foo finished and the terminal is closed.但是如果 bar 中的时间较大,那么当 foo 完成并且终端关闭时 bar 将被杀死。

Any idea how to set up correctly a background instance of bar.sh?知道如何正确设置 bar.sh 的背景实例吗? Another terminal which stays open is not a solution to me.另一个保持打开状态的终端对我来说不是解决方案。 THX!谢谢!

This answer comes from learner on Ask Ubuntu :这个答案来自Ask Ubuntu上的学习者:

"nohup" stands for "No Hungup". “nohup”代表“不挂断”。 As the name suggests, it will continue to run the command without hanging up, even if session is disconnected.顾名思义,即使session断开连接,它也会继续运行命令而不挂机。 It is used to run commands on remote server which would take way long time to complete (backing up a DB etc).它用于在远程服务器上运行需要很长时间才能完成的命令(备份数据库等)。

Usually, the logs of script (if any) are stored in ~/nohup.out file.通常,脚本的日志(如果有)存储在 ~/nohup.out 文件中。 But you can choose to capture the logs, either by appending or replacing the logs of old contents with the new.但是您可以选择捕获日志,方法是使用新内容附加或替换旧内容的日志。 ">" Single greater than sign will replace old logs with the new. ">" 单个大于号将用新日志替换旧日志。 ">>" double greater than sign will append the logs to the end of the file. “>>”双大于号将 append 的日志放到文件末尾。

I'm using this below format to run the same thing in crontab.我正在使用以下格式在 crontab 中运行相同的内容。 In this case, I'm replacing the old logs.在这种情况下,我将替换旧日志。 /path/to/script_folder/script_name.sh > /path/to/script_folder/logs/script_name.log 2>&1 /path/to/script_folder/script_name.sh > /path/to/script_folder/logs/script_name.log 2>&1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM