LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian
User Name
Password
Debian This forum is for the discussion of Debian Linux.

Notices


Reply
  Search this Thread
Old 01-14-2014, 10:35 AM   #1
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 658

Rep: Reputation: 35
Question Init.d for application that doesn't detach from terminal?


Hello

Title says it all: In Debian, I need to find a way to launch an application at boot time that doesn't normally run daemonized.

Here's what it looks like in Ubuntu:
Code:
description "My non-daemon app"
start on stopped rc
stop on runlevel [016]
respawn
script
/usr/bin/myapp -document_root /var/www
end script
post-stop exec sleep 3
I tried using an existent init.d script and just adding a trailing "&" on the relevant line in the "start" section, but it doesn't look right: Unlike other daemons, "ps aux" show both the application and the init.d script:
Code:
#! /bin/sh
### BEGIN INIT INFO
# Provides:          myapp
# Required-Start:    $syslog $time $remote_fs
# Required-Stop:     $syslog $time $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: My app
# Description:       Debian init script for my app
### END INIT INFO

PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/sbin/myapp
PIDFILE=/var/run/myapp.pid

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

case "$1" in
  start)
        log_daemon_msg "Starting My app" "myapp"
        start_daemon -p $PIDFILE $DAEMON &
        log_end_msg $?
    ;;
  stop)
        log_daemon_msg "Stopping Myapp" "myapp"
        killproc -p $PIDFILE $DAEMON
        log_end_msg $?
    ;;
  force-reload|restart)
    $0 stop
    $0 start
    ;;
  status)
    status_of_proc -p $PIDFILE $DAEMON atd && exit 0 || exit $?
    ;;
  *)
    echo "Usage: /etc/init.d/myapp {start|stop|restart|force-reload|status}"
    exit 1
    ;;
esac

exit 0
Is there a correct way to write an init.d script so that the app is launched with the rest of them?

Thank you.
 
Old 01-14-2014, 01:52 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
As you probably know, the exec command (bash builtin) will allow the child process to take over the PID from its parent (i.e. per the manpages, the command specified "replaces the shell").

For a rather different approach, you can run your command (even from an init script) in batch using the at(1) daemon, a la:
Code:
at now <<< '/your/non-daemon-script/here'
 
Old 01-15-2014, 07:52 AM   #3
littlebigman
Member
 
Registered: Aug 2008
Location: France
Posts: 658

Original Poster
Rep: Reputation: 35
Thanks. What would be the right way to edit the init.d script and use "exec" instead of "&" to launch the application?
 
Old 01-15-2014, 09:46 AM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
I just ran through a contrived experiment that (I think) will produce the effect you're looking for.

run-it.bash
Code:
#!/bin/bash

PATH=/usr/bin:/bin

# Note: use the FQ path here; my test
# makes assumptions about cwd.
#
exec ./test.bash &

exit 0
test.bash
Code:
#!/bin/bash

PATH=/usr/bin:/bin

touch /tmp/first-touch

sleep 120

touch /tmp/second-touch

exit 0
Execution and results:
Code:
[hk] ~> ./run-it.bash

[hk] ~> ps -ef | grep -E '[t]est\.bash|[r]un-it\.bash'
eriks    18005     1  0 09:43 pts/5    00:00:00 /bin/bash /home/eriks/test.bash

[hk] ~> ls -l /tmp/first-touch /tmp/second-touch
-rw-r--r-- 1 eriks eriks 0 Jan 15 09:43 /tmp/first-touch
-rw-r--r-- 1 eriks eriks 0 Jan 15 09:45 /tmp/second-touch
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can't detach from screen session which runs java application joe_2000 Programming 12 03-16-2013 06:46 PM
Ubutnu won't boot. Error: Target file system doesn't have /sbin/init. No init found. Zeljka_Lin Linux - Newbie 9 05-02-2011 06:56 AM
[SOLVED] Automatic Startup and Shutdown of application --- /etc/init.d/ vikas027 Programming 4 08-17-2009 04:34 PM
[SOLVED] how to start an application using the rc.local init script? sl33p Linux - Newbie 4 07-07-2009 04:06 PM
How to start a program in terminal and let it 'detach' neo_in_matrix Linux - Newbie 2 04-12-2005 05:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian

All times are GMT -5. The time now is 02:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration