Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can daemonize any executable in Unix by using nohup and the &amp; operator:</p> <pre><code>nohup yourScript.sh script args&amp; </code></pre> <p>The nohup command allows you to shut down your shell session without it killing your script, while the &amp; places your script in the background so you get a shell prompt to continue your session. The only minor problem with this is standard out and standard error both get sent to ./nohup.out, so if you start several scripts in this manor their output will be intertwined. A better command would be:</p> <pre><code>nohup yourScript.sh script args &gt;script.out 2&gt;script.error&amp; </code></pre> <p>This will send standard out to the file of your choice and standard error to a different file of your choice. If you want to use just one file for both standard out and standard error you can us this:</p> <pre><code>nohup yourScript.sh script args &gt;script.out 2&gt;&amp;1 &amp; </code></pre> <p>The 2>&amp;1 tells the shell to redirect standard error (file descriptor 2) to the same file as standard out (file descriptor 1).</p> <p>To run a command only once and restart it if it dies you can use this script:</p> <pre><code>#!/bin/bash if [[ $# &lt; 1 ]]; then echo "Name of pid file not given." exit fi # Get the pid file's name. PIDFILE=$1 shift if [[ $# &lt; 1 ]]; then echo "No command given." exit fi echo "Checking pid in file $PIDFILE." #Check to see if process running. PID=$(cat $PIDFILE 2&gt;/dev/null) if [[ $? = 0 ]]; then ps -p $PID &gt;/dev/null 2&gt;&amp;1 if [[ $? = 0 ]]; then echo "Command $1 already running." exit fi fi # Write our pid to file. echo $$ &gt;$PIDFILE # Get command. COMMAND=$1 shift # Run command until we're killed. while true; do $COMMAND "$@" sleep 10 # if command dies immediately, don't go into un-ctrl-c-able loop done </code></pre> <p>The first argument is the name of the pid file to use. The second argument is the command. And all other arguments are the command's arguments.</p> <p>If you name this script restart.sh this is how you would call it:</p> <pre><code>nohup restart.sh pidFileName yourScript.sh script args &gt;script.out 2&gt;&amp;1 &amp; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload