Is it correct to use the rc.local file to start a program when the system starts? - linux

I want to start a java program ( class ) when the system starts ( even though when no user has logged in the system ). I read some documentation and I found that Linux executes the init command when booting and executes the scripts in the rc.d directory. And the rc.local file is executed last. So I wonder if it is right to edit this file in order to run an application when the system has finished booting up ?

It depends. If your program
is a purely local modification and
you don't need daemon control over the software (start/stop it later at system run) and
a simple SIGTERM suffices to stop your program on system shutdown,
then yes, rc.local is just the point to put it.
If you intend to install the file on other systems, put a file in /etc/init.d and use the system specific commands to let it run at the appropriate runlevels.

Related

Start script on linux startup

I try to execute a java application at startup in a yocto based linux device. I added a script at /etc/init.d/etic and made it executable. If I call at the shell /etc/init.d/etic start or /etc/init.d/etic stop the application is started an stopped as expected. Then I called on the shell update-rc.d etic defaults and the symlinks were created. According to what I found on the web, this should be enough, but somehow the application is not started. What did I miss? How could I check what is going wrong or is there any minimal example which should work which I can try to extend?
Well, often such issues are due to a different environment when executing the start script by hand, as compared to when it's being run from the init system. For instance, your .profile and .bashrc won't have been sourced, when running from the init system.
You can use eg logger to easily log things from your init-script, and this rather easily find out what goes wrong.

How to set up a bash script to run in the background anytime the linux ubuntu server is running

I have written up a simple bash script that will copy the newest image from my ip camera into a directory, rename the file and delete the old file. The script loops every 10 seconds.
I want to have this script start running in the background and run continuously all the time that the server is up.
I understand the part about adding a & to the end of the command will cause it to run in the background.
Is init.d the best place to execute this?
I am running ubuntu server.
This sort of thing is normally done by service scripts, which you would find under /etc/init.d. Depending on the version, that might be a "System V init script", or one of the systemd scripts.
A simple service script of the sort you are asking about would start automatically (based on comments in the script's header that tell what run-levels it would use), create a file under /var/run telling what process-id the script uses (to allow killing it), and run the copying in a loop, calling sleep 10 to space the timing as indicated.
A typical service script should implement "start", "stop", "restart" and "status". Not all do, but there is rarely a good reason to not do this.
On my (Debian) system, there is a README file in the directory which is a good introduction to the topic. There are several tutorials available for the topic. Here are a few:
Linux: How to write a System V init script to start, stop, and restart my own application or service
Writing a Linux Startup Script
Manage System Startup and Boot Processes on Linux with Upstart

Linux Invoke custom script during OS shutdown

I wrote a simple JAVA application which runs as a service. When my application is up and running, I maintain the PID in a file which will be used while stopping the application.
Issue:
When I restart the OS the stop script is not called (not sure how to make this happen) and the old PID is left as it is in the PID file. Now, after reboot (which start my app) when I stop the app using stop script now my stop script will try to clean up all the PID listed in the file. Most of the time, I will get "No such process". But there are chance the same PID might have been used for some other process
Question:
How I can make sure my stop script will be invoked when I shutdown ore reboot the OS? I am looking a solution for RHEL environment.
I think your are looking for a init script. (startup/shutdown services at different run levels)
This is a good reference
http://blog.rimuhosting.com/2009/09/30/one-java-init-script/
this has a good refernce to Linux init.d scripts
http://coreymaynard.com/blog/creating-a-custom-initd-script-on-fedora/
Hope it helps
If you are looking for scripts that run after reboot, I guess you can write the script in /etc/rc.local and then you can start your service.
This script will run after all your init scripts have run while your machine starts. Using this you can delete the old PID file.

Linux: Makefile code that adds a daemon to startup scripts

I'm currently designing a program that I want to run when my computer boots up. I've written a daemon that runs the code, so what I want is a way to run the daemon on startup. Here's my problem: I don't want to my end user to have to manually add the daemon into their list of startup scripts, I want the daemon to be added automatically when the program is installed. At the moment I have a makefile that is installing the software to my pc, so is there a way to get the makefile to insert the daemon into the user's list startup scripts, so the daemon is run on startup?
Cheers
You could have some target (perhaps installcron) in your Makefile that would use the crontab(1) command to add (perhaps after verifying it is not there already) a crontab(5) entry for #reboot (which is run at boot time).

Starting a process when Linux starts (Ubuntu)

I have a process (Spark chat client) which needs to be run when my Ubuntu boots up. For this I have done followings.
I created a run.sh file which will fire up my application (and I check it's working)
I created a symbolic link from both /etc/rc5.d/ and /etc/rc3.d/ to my run.sh file. (A symbolic link is also working fine)
But my processes don't start up when my machine boots. (Is this the way to do it or am I doing the wrong thing here?)
I'm running on Ubuntu 10.04 LTS (Lucid Lynx).
Your solution would've worked in most Linux distributions. However, Ubuntu never goes past runlevel 2.
Just in case, this means the contents of rc?.d with ? > 2 are not used unless you manually raise the runlevel as root. Use rc2.d :)
The symlinks you created in /etc/rc5.d/ and /etc/rc3.d/ should be named S##name. S is for start, and the number ## gives an order in which the scripts are run.
Note also that the symlinks in these directories usually points to the actual script located in /etc/init.d/.
It looks like you want to run an X program when a user logs in, not a service on startup. Remember, in Linux there is no GUI; X is a program that runs to display graphics on the screen.
You likely want to set up a program to start on KDE/Gnome login. Each has their own way to do it, but is generally boils down to pointing at a script and saying "Run this."
Put the command to run that script in the /etc/rc.local file. I think it will run each time you log in to the system.

Resources