Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6690173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:36:20+00:00 2026-05-26T05:36:20+00:00

I wrote a program and now I am expected to convert it to a

  • 0

I wrote a program and now I am expected to convert it to a daemon. Since I am a noob and dont know anything about it, can you please tell me how can I do it or can you please give me basic tutorials or readings about it ? I really want to learn how I can do it ?

Thank you all

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:36:20+00:00Added an answer on May 26, 2026 at 5:36 am

    Java class:

    package example;
    
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;
    import org.apache.commons.daemon.*;
    
    class EchoTask extends TimerTask {
        @Override
        public void run() {
            System.out.println(new Date() + " running ...");
        }
    }
    
    public class Main implements Daemon {
    
        private static Timer timer = null;
    
        public static void main(String[] args) {
            timer = new Timer();
            timer.schedule(new EchoTask(), 0, 1000);
        }
    
        @Override
        public void init(DaemonContext dc) throws DaemonInitException, Exception {
            System.out.println("initializing ...");
        }
    
        @Override
        public void start() throws Exception {
            System.out.println("starting ...");
            main(null);
        }
    
        @Override
        public void stop() throws Exception {
            System.out.println("stopping ...");
            if (timer != null) {
                timer.cancel();
            }
        }
    
        @Override
        public void destroy() {
            System.out.println("done.");
        }
    
     }
    

    Shell start/stop script:

    #!/bin/sh
    
    # Setup variables
    EXEC=/usr/bin/jsvc
    JAVA_HOME=/usr/lib/jvm/java-6-sun
    CLASS_PATH="/usr/share/java/commons-daemon.jar":"/path/to/your.jar"
    CLASS=example.Main
    USER=foo
    PID=/tmp/example.pid
    LOG_OUT=/tmp/example.out
    LOG_ERR=/tmp/example.err
    
    do_exec()
    {
        $EXEC -home "$JAVA_HOME" -cp $CLASS_PATH -user $USER -outfile $LOG_OUT -errfile $LOG_ERR -pidfile $PID $1 $CLASS
    }
    
    case "$1" in
        start)
            do_exec
                ;;
        stop)
            do_exec "-stop"
                ;;
        restart)
            if [ -f "$PID" ]; then
                do_exec "-stop"
                do_exec
            else
                echo "service not running, will do nothing"
                exit 1
            fi
                ;;
        *)
                echo "usage: daemon {start|stop|restart}" >&2
                exit 3
                ;;
    esac
    

    And the effect:

    $ ./service start && sleep 5 && ./service stop
    $ cat /tmp/example.out
    initializing ...
    starting ...
    Fri Oct 07 16:26:54 EEST 2011 running ...
    Fri Oct 07 16:26:55 EEST 2011 running ...
    Fri Oct 07 16:26:56 EEST 2011 running ...
    Fri Oct 07 16:26:57 EEST 2011 running ...
    Fri Oct 07 16:26:58 EEST 2011 running ...
    stopping ...
    done.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a program in c# now I would like to know what is
I wrote a program in C with Ubuntu Linux and now I need to
I wrote a program in python (using standard python libraries) long ago. Now I
I wrote a program and now I want to calculate the total running time
I wrote a program with java and mysql and now i want to have
A few month a go i wrote a program, now when i try to
I'm trying to write a program with Hibernate. My domain is now complete and
I wrote a program which includes writing and reading from database. When I run
I wrote a program using AutoIT to fetch information from a number of websites
I wrote a program out, which was all in one file, and the methods

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.