简体   繁体   English

如何在启动时运行两个Shell脚本?

[英]How to run two shell scripts at startup?

I am working with Ubuntu 16.04 and I have two shell scripts: 我正在使用Ubuntu 16.04,并且有两个Shell脚本:

  1. run_roscore.sh : This one fires up a roscore in one terminal. run_roscore.sh:这在一个终端中启动一个roscore。
  2. run_detection_node.sh : This one starts an object detection node in another terminal and should start up once run_roscore.sh has initialized the roscore. run_detection_node.sh:这个启动另一个终端中的对象检测节点,并且应该在run_roscore.sh初始化roscore时启动。

I need both the scripts to execute as soon as the system boots up. 我需要两个脚本在系统启动后立即执行。

I made both scripts executable and then added the following command to cron: @reboot /path/to/run_roscore.sh; /path/to/run_detection_node.sh 我使两个脚本@reboot /path/to/run_roscore.sh; /path/to/run_detection_node.sh执行,然后将以下命令添加到cron: @reboot /path/to/run_roscore.sh; /path/to/run_detection_node.sh @reboot /path/to/run_roscore.sh; /path/to/run_detection_node.sh , but it is not running. @reboot /path/to/run_roscore.sh; /path/to/run_detection_node.sh ,但是它没有运行。

I have also tried adding both scripts to the Startup Applications using this command for roscore: sh /path/to/run_roscore.sh and following command for detection node: sh /path/to/run_detection_node.sh . 我还尝试使用以下命令将这两个脚本添加到“ 启动应用程序”中 :roscore: sh /path/to/run_roscore.sh以及以下用于检测节点的命令: sh /path/to/run_detection_node.sh And it still does not work. 而且它仍然不起作用。

How do I get these scripts to run? 如何使这些脚本运行?

EDIT: I used the following command to see the system log for the CRON process: grep CRON /var/log/syslog and got the following output: 编辑:我使用以下命令来查看CRON进程的系统日志: grep CRON /var/log/syslog并获得以下输出:

CRON[570]: (CRON) info (No MTA installed, discarding output) . CRON[570]: (CRON) info (No MTA installed, discarding output)

So I installed MTA and then systemlog shows: CRON[597]: (nvidia) CMD (/path/to/run_roscore.sh; /path/to/run_detection_node.sh) 所以我安装了MTA,然后系统日志显示: CRON[597]: (nvidia) CMD (/path/to/run_roscore.sh; /path/to/run_detection_node.sh)

I am still not able to see the output (which is supposed to be a camera stream with detections, as I see it when I run the scripts directly in a terminal). 我仍然看不到输出(应该是带有检测的摄像机流,就像我在终端中直接运行脚本时看到的那样)。 How should I proceed? 我应该如何进行?

Since I got this working eventually, I am gonna answer my own question here. 自从我最终完成这项工作以来,我将在这里回答我自己的问题。

I did the following steps to get the script running from startup: 我执行了以下步骤来使脚本从启动运行:

  • Changed the type of the script from shell to bash (extension .bash ). 将脚本的类型从shell更改为bash(扩展名.bash )。
  • Changed the shebang statement to be #!/bin/bash . 将shebang语句更改为#!/bin/bash
  • In Startup Applications, give the command bash path/to/script to run the script. 在“启动应用程序”中,给命令bash path/to/script运行脚本。

Basically when I changed the shell type from sh to bash , the script starts running as soon as the system boots up. 基本上,当我将shell类型从sh更改为bash ,脚本会在系统启动时立即开始运行。

Note, in case this helps someone: My intention to have run_roscore.bash as a separate script was to run roscore as a background process. 请注意,如果这对某人有帮助:我希望将run_roscore.bash作为单独的脚本运行是将roscore作为后台进程运行。 One can run it directly from a single script (which is also running the detection node) by having roscore& as a command before the rosnode starts. 通过在rosnode启动之前将roscore roscore&作为命令,可以从单个脚本(也正在运行检测节点)直接运行它。 This command will fire up the master as a background process and leave the same terminal open for following commands to be executed. 该命令将启动主机作为后台进程,并保持同一终端打开以执行以下命令。

If you could install immortal you could use the require option to start in sequence your services, for example, this is could be the run config for /etc/immortal/script1.yml : 如果您可以安装不朽 ,则可以使用require选项按顺序启动服务,例如,这可以是/etc/immortal/script1.yml运行配置:

cmd: /path/to/script1
log:
    file: /var/log/script1.log
wait: 1
require:
  - script2

And for /etc/immortal/script2.yml 对于/etc/immortal/script2.yml

cmd: /path/to/script2
log:
    file: /var/log/script2.log

What this will do it will try to start both scripts on boot time, the first one script1 will wait 1 second before starting and also wait for script2 to be up and running, see more about the wait and require option here: https://immortal.run/post/immortal/ 这将完成它会尝试启动开机时间这两个脚本,第一个script1将等待1秒开始之前,也等待script2要运行起来,看到更多的waitrequire在此选项: https://开头immortal.run/post/immortal/

Based on your operating system you will need to configure/setup immortaldir , her is how to do it for Linux: https://immortal.run/post/how-to-install/ 根据您的操作系统,您将需要配置/设置immortaldir ,这是在Linux上的配置方法: https : //immortal.run/post/how-to-install/

Going more deep in the topic of supervisors there are more alternatives here you could find some: https://en.wikipedia.org/wiki/Process_supervision 要深入探讨主管的话题,您可以在这里找到更多替代方案: https : //en.wikipedia.org/wiki/Process_supervision

If you want to make sure that "Roscore" (whatever it is) gets started when your Ubuntu starts up then you should start it as a service (not via cron). 如果要确保在Ubuntu启动时启动“ Roscore”(无论它是什么),则应将其作为服务启动(而不是通过cron)。 See this question/answer . 请参阅此问题/答案

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

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