Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to create an entirely automated box (based on something like a Raspberry-Pi or OLinuXino card).

My box would run a Debian based system with a read-only filesystem and a ramdisk (to avoid SD card corruptions).

I would like to create a script that automatically connects to a remote server (via ssh) each time the box is switched on or plugged to a network.

To do so, I think my simplest solution is to create a shell script running as a daemon. This daemon will have to stay alive until the box is shut down or disconnected from network. The script should be able to reconnect when network is up or box restarts.

The question: do you think a shell script is safe enough to perform this ? Or should I better use another (and safer) programming language (PERL, C, ...) ?

share|improve this question

Many Daemons are run using bash as the scripting language. You won't have any issues using it.

share|improve this answer
any daemons in particular...? – mikeserv Jul 31 '14 at 3:44
I want to be sure: I want the daemon program to be written using bash, not only to be ran from a bash script. – lauhub Jul 31 '14 at 23:55

As a general rule, it is quite tricky to create a daemon in bash (or shell-script in general). Some problems like background fork, output and error messages redirection or signal handling are not easy to get right.

For the specific problem you describe there is already some (free) software doing automatic ssh connexion with respawn.

I use autossh for this purpose. It is packaged in Debian and works great.

share|improve this answer
fyi background forks in bash are as easy as encasing the command in parentheses. Output and error messages are equally as easy using bash. I guess it all depends how well you know the scripting language. As far as using autossh instead, I second that motion! – Jeight Jul 30 '14 at 21:51
I have seen bash rather effectively used as server monitoring and firewall daemon, its not that useless... – Wilf Jul 30 '14 at 21:57
Ok, I'll take a look at autossh. Thanks – lauhub Jul 31 '14 at 10:39

It is quite safe, but it can be less than useful unless bash is in a position to handle the cleanup. If it is killed for any reason, the script needs to be capable of execing itself in order to refresh its state as necessary - you probably don't want to simply do:

while :

...for instance. That gets old pretty fast. Instead maybe do something like:

trap '' HUP EXIT
set_state() { echo stuff="'$(otherstuff)'"... ; }
chk_interval() { wait for something... ; }
exec_me() { exec env - $(set_state) "$0" <in >out 2>alsoout & ; }

It can also depend on who is doing the cleanup. The simplest means of implementing a daemon is like:


...on the kernel commandline for particularly single-purposed installations. Where the relative / inferred here is the initramfs /.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.