InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
“Daemon-izing” Your Script in bash
Bash

“Daemon-izing” Your Script in bash

InfinityCoder February 20, 2017

Sometimes you want a script to run as a daemon, i.e., in the background and never ending.

To do this properly you need to be able to detach your script from its controlling tty, that is from the terminal session used to start the daemon.

Simply putting an ampersand on the command isn’t enough.

If you start your daemon script on a remote system via an SSH (or similar) session, you’ll notice that when you log out, the SSH session doesn’t end and your window is hung until that script ends (which,
being a daemon, it won’t).

Use the following to invoke your script, run it in the background, and still allow yourself to log out:

1
nohup mydaemonscript 0<&- 1>/dev/null 2>&1 &

or:

1
nohup mydaemonscript >>/var/log/myadmin.log 2>&1 <&- &

You need to close the controlling tty, which is connected in three ways to your (or any) job: standard input (STDIN), standard output (STDOUT), and standard error (STDERR).

We can close STDOUT and STDERR by pointing them at another file— typically either a log file, so that you can retrieve their output at a later time, or at the file /dev/null to throw away all their output.

We use the redirecting operator > to do this.
But what about STDIN? The cleanest way to deal with STDIN is to close the file descriptor.

The bash syntax to do that is like a redirect, but with a dash for the filename (0<&- or <&-).
We use the nohup command so that the script is run without being interrupted by a hangup signal when we log off.
In the first example, we use the file descriptor numbers (i.e., 0, 1, 2) explicitly in all three redirections.

They are optional in the case of STDIN and STDOUT, so in oursecond example we don’t use them explicitly.

We also put the input redirect at the end of the second command rather than at the beginning, since the order here is not important.

(However, the order is important and the file descriptor numbers are necessary in redirecting STDERR.)

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Finding Files by Date in bash
Suppose someone sent you a JPEG image file that you …

Finding Files by Date in bash

Sending Both Output and Error Messages to Different Files in bash
You are expecting output from a program but you don’t …

Sending Both Output and Error Messages to Different Files in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap