I have no idea where to start looking. I've been reading about daemons and didn't understand the concept.
More details :
- I've been writing a crawler which never stops and crawlers over RSS in the internet.
- The crawler has been written in java - therefore its a jar right now.
- I'm an administrator on a machine that has Ubuntu 11.04 .
- There is some chances for the machine to crash , so I'd like the crawler to run every time you startup the machine.
- Furthermore, I'd like it to keep running even when i logged out. I'm not sure this is possible, but most of the time I'm logged out, and I still want to it crawl.
Any ideas? Can someone point me in the right direction?
Just looking for the simplest solution.
7 Answers
Here's a easy way to do that using SysVInit. Instructions:
Create the start and the stop script of your application. Put it on some directory, in our example is:
- Start Script:
/usr/local/bin/myapp-start.sh - Stop Script:
/usr/local/bin/myapp-stop.sh
Each one will provide the instructions to run/stop the app. For instance the
myapp-start.shcontent can be as simple as the following:#!/bin/bash java -jar myapp.jarFor the stop script it can be something like this:
#!/bin/bash # Grabs and kill a process from the pidlist that has the word myapp pid=`ps aux | grep myapp | awk '{print $2}'` kill -9 $pid- Start Script:
Create the following script (
myscript) and put it on/etc/init.d./etc/init.d/myscriptcontent:#!/bin/bash # MyApp # # description: bla bla case $1 in start) /bin/bash /usr/local/bin/myapp-start.sh ;; stop) /bin/bash /usr/local/bin/myapp-stop.sh ;; restart) /bin/bash /usr/local/bin/myapp-stop.sh /bin/bash /usr/local/bin/myapp-start.sh ;; esac exit 0Put the script to start with the system (using SysV). Just run the following command (as root):
update-rc.d myscript defaults
PS: I know that Upstart is great and bla bla, but I preffer the old SysV init system.
8Yes! It is possible. :) Upstart is the way to go to make sure the service stays running. It has five packages, all installed by default:
- Upstart init daemon and initctl utility
- upstart-logd provides the logd daemon and job definition file for logd service
- upstart-compat-sysv provides job definition files for the rc tasks and the reboot, runlevel, shutdown, and telinit tools that provide compatibility with SysVinit
- startup-tasks provides job definition files for system startup tasks
- system-services provides job definition files for tty services
The learning is very enjoyable and well worth it. Upstart has a website:
13 quick suggestions...
Create a Start script in
/etc/rc3.d(multiuser console mode) with corresponding Kill scripts in/etc/rc.0and/etc/rc6.dto kill your Java program in a controlled way when the system powers down (runevel 0) or reboots (runlevel 6) See An introduction to Runlevels.You might be able to start your Java app in runlevel 2 (rc2.d) but, as a crawler, it will need TCP/IP. So make sure your networking service is available/started in your runlevel 2 beforehand. Networking is definitely up in runlevel 3.
/etc/init.dcontains all the actual start/kill scripts./etc/rcN.ddirectories just contain links to them, prefixed with S or K to start or kill them respectively, per runlevel N.A process run by
crondshould persist between logouts. Maybe add it to your crontab.A process run with
nohupshould also persist. See nohup: run a command even after you logout.$ nohup java -jar myapp.jar &By default,
myapp.jar's standard output will go to a file named./nohup.out, or$HOME/nohup.outif the former isn't writeable.
Also be aware when making aplications with:
update-rc.d myscript defaults To have permissions 0755 and to use or get before your .sh the path.
Let's say your script is on /root/test.sh , you should first change to CD /root/ before accessing test.sh.
So best way is to make a SH on init.d and there change the path to your sh and execute it in the start function.
Simple startup scripts with Java version checking for Linux auth-agentand Windows auth-agent.bat
The easiest way is to use supervisord. Please see full details at
You can also view more details here:
Running an executable jar file when the system starts
I prefer systemd service and this is a sample for Quarkus application startup service and this is like any java application.
go to
/etc/systemd/systemcreate
Quarkus.servicefilefill the file like this example.
[Unit]
Description=Quarkus Application
Wants=syslog.target network.target remote-fs.target
After=remote-fs.target
Before=multi-user.target
[Service]
User=test
LimitNOFILE=102642
Environment="QUARKUS_MAILER_PASSWORD=password1"
Environment="QUARKUS_DATASOURCE_USERNAME=password2"
Environment="QUARKUS_DATASOURCE_PASSWORD=password3"
ExecStart=/usr/lib/jvm/java-11-openjdk/bin/java -jar /path/to/jar file/target/quarkus-app/quarkus-run.jar
ExecStop=/bin/kill -15 $MAINPID
Type = simple
[Install]
WantedBy=multi-user.target- type
systemctl daemon-reload systemctl enable quarkus.servicesystemctl start quarkus.service