How to start application after login on CentOS? - linux

I am trying to start GUI application with upstart script on CentOS. I have test script located /etc/init/ folder.
start on desktop-session-start
stop on desktop-shutdown
respawn
script
export DISPLAY=:0
sleep 5
exec /.1/Projects/UpstartTest/start.sh &
end script
start.sh scripts is running binary files for GUI application.
After reboot my computer. When I typed:
[root#mg-CentOS ~]# initctl status test
test stop/waiting
So my upstart is not runnig. When i type
initctl start test
manually it works fine without any problem.
How can I run this upstart script after user login (desktop started) ? I am trying to find detailed documents for CentOS for upstart but there is no.

For this purpose, you can use the update-rc tool, which is builtin on linux distributions. It basically creates a symbolic link for the script you want to be executed at startup, or some other OS states, on the folder rc.X, where X is the number of the folder that determines a state that you want.
You may want to have a look at this answer: Update-rc.d custom script running too late, and also runs at shutdown
More information about can be found here:
http://www.linux.com/news/enterprise/systems-management/8116-an-introduction-to-services-runlevels-and-rcd-scripts
Detailed information about the CentOS booting process can be found here: https://www.centos.org/docs/5/html/Installation_Guide-en-US/s1-boot-init-shutdown-process.html;
the rc is being explained at this document as well.

Related

How to execute automatically command at rapsbian startup

I want my raspberry to execute 2 commands when he starts but i don't know how to setup it
the commands are
cd /var/www/restaurant && php -S 10.0.0.1:8000 -t public
i have tried to edit /etc/rc.local and add my command but it doesn't work
The "official" way to run a program at boot time on systemd-based Raspbian systems (and, in fact, most modern Linux distributions) is to create a systemd unit file. There are specific instructions for Raspbian here:
https://www.raspberrypi.org/documentation/linux/usage/systemd.md
The entry After=network.target is particular relevant in this case, because I imagine your program will need network interfaces to be up.
rc.local is a hold-over from the SysV init days, and I've heard reports of it not working reliably in Raspbian. Creating a systemd unit file provides a simple way to test the service using systemctl without actually having to reboot. If it fails on boot, you'll probably need to use journalctl to see the error messages.

Daemonize a perl script at startup Linux

I have a custom version of CENTOS that I need to run a perl script as a daemon in at all times.
To do this I want to get it to run on startup as a daemon.
When the machine is on I can daemonize the script with the command
daemonize /var/myfolder/myscript.pl
And this works fine.
So I have an rc1 script which has a number of commands that run when the machine starts, and the very last line in it is where I try to daemonize that script. Everything else in the rc1 script runs fine, and the script doesn't output any errors, however when I check to see if the daemon is running on start up, it isn't running.
Is there another way that I can get the script to run on startup? Or any ideas on why this method doesn't work?
Proc::Daemon offers what you're looking for.
See this previously asked question: How can I run a Perl script as a system daemon in linux?
The problem was that #INC hadn't fully loaded all of the modules by the time my script was called, and so my daemon wasn't running. I used the PERL5LIB command to add the appropriate directories.
PERL5LIB='/perl:/custom/lib:/usr/local/lib64/perl5' /var/myfolder/myscript.pl &
where /perl; /custom/lib and /usr/local/lib64/perl5 are the directories which were missing from #INC

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.

Long running java process

I have inherited a java system which should be run in the background on a Linux server. The directions call for it to be started java -jar start.jar. This seems a little too fragile for my liking. I'd like it to actually run in the background, ideally starting up automatically at boot time.
What is the best way to achieve this? I've looked into running the system within a screen environment, which works fine, but won't automatically start up when the system reboots.
You can write an init.d script for it! (init.d scripts are started in specified runlevels at startup time)
Or you can start it in the background with & in the end of the command.
java -jar start.jar &
What kind of distribution do you use? Debian?
Have a look at: Creating your own init.d script
At least on ubuntu, I'd put this command in the /etc/rc.local file with an & at the end.
+1 on using an init.d script
You should also consider using jsvc (http://commons.apache.org/daemon/jsvc.html) in your init.d script to allow you to use privileged resources while running the service as an unprivileged user. jsvc is bundled with at least Debian and Ubuntu.

Resources