Set service to start on boot in Linux

In Linux, sometimes after updating, services will no longer start on boot – or their init level goes back to 0. This just means the service will no longer start at boot (or on login, or anything else other than if you manually ran service whatever start)

To fix this, we can just check the config level and then add it to runlevels 2, 3, 4, 5 – which means it will start on boot. 5 just gives it access to X, so if it’s an app with a GUI, it will HAVE to be on level 5 too.

If you want to read more about runlevels, read here.

$ chkconfig --list sshd

In CentOS it will usually come back with something like this:

sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off

So you see ssh is set to start on boot. What if it returns back something like this?

sshd            0:off   1:off   2:off    3:off    4:off    5:off    6:off

Well, then we need to fix it so it starts on boot. That can be done like this (as root):

$ chkconfig --level 2345 sshd on

That will restore it or put it at all those run levels – so if you reboot, it will start on boot.

How to install an .rpm from the terminal

Installing a .rpm package from the terminal is a little different than installing a similar package as .deb in a different distro.

There are a few different commands, all of which start with “rpm”. This command will install a package:

$ sudo rpm -ivh packagename.rpm

This will “upgrade” a package:

$ sudo rpm -Uvh packagename.rpm

And if you want to find out what dependencies an .rpm has, you can run this against your .rpm:

$ sudo rpm -qpR packagename.rpm

It’s fairly easy and not complicated, but it’s still a question I see / hear a lot.

Web Server “Essential Services” Monitoring Script

A while back I wrote a script that I set as a cron (every 5 minutes) to monitor my web server (LAMP with mod_proxy / BIND). I was having trouble, specifically with BIND, with few or no logs (I changed this, but they were so non-descript that I had no idea why it was dying). Thankfully an update came out for it via repo that fixed that issue, but not before I’d written a script to do it for me. So I present, the Web Monitoring Script.

This was written for Debian, but a few easy modifications to your variables will have it working on CentOS / RedHat / Fedora.

Prerequisites – You need root to add this as a cron. You also need to create this folder “/var/log/server_monitor/”.

It would also be a good idea to do set up log rotation on those logs.

To do that, follow this (and use whatever text editor you like, my server doesn’t have a GUI, so I tend to use text based tools):

# cd /etc/logrotate.d
nano server_monitor

And add this to that file and save.

/var/log/server_monitor/*.log {
	weekly
	missingok
	rotate 5
	compress
	notifempty
	create 640 root adm
	sharedscripts
	postrotate
	endscript
}

Now your logs will rotate weekly and will be compressed.

Now on to the actual script:

#!/bin/bash
#
# This is a server monitor that will run every 5 minutes to be sure all services are running, and if they are not, to start them.
#
# Let's set some variables quick
############################################################
RESTARTAPACHE="/etc/init.d/apache2 restart"
RESTARTBIND="/etc/init.d/bind9 restart"
RESTARTMYSQL="/etc/init.d/mysql restart"
RESTARTXINETD="/etc/init.d/xinetd restart"
RESTARTQMAIL="/etc/init.d/qmail restart"
############################################################
############################################ Path to pgrep
PGREP="/usr/bin/pgrep"
############################################################
############################################ Program names
APACHE2="apache2"
BIND9="named"
MYSQL="mysql"
XINETD="xinetd"
QMAIL="qmail"
############################################################
########################################## Set the log file
LOG="/var/log/server_monitor/server_monitor.log"
SUCCESS=" was down and has restarted successfully at $(date)"
###########################################################
############################################### Set date
set $(date)
#
# Apache
#
$PGREP $APACHE2
if
	[ $? -ne 0 ] #This shows that apache is not running since there are no pids
then
	$RESTARTAPACHE
	echo "$APACHE2 $SUCCESS" >> $LOG
else
	echo "$APACHE2 is running fine at $(date)" >> $LOG
fi;
#
# Bind
#
$PGREP $BIND9
if
	[ $? -ne 0 ]
then
	$RESTARTBIND
	echo "$BIND9 $SUCCESS" >> $LOG
else
	echo "$BIND9 is running fine at $(date)" >> $LOG
fi;
#
# MySQL
#
$PGREP $MYSQL
if
	[ $? -ne 0 ]
then
	$RESTARTMYSQL
	echo "$MYSQL $SUCCESS" >> $LOG
else
	echo "$MYSQL is running fine at $(date)" >> $LOG
fi;
#
# Xinetd
#
if
	[ $? -ne 0 ]
then
	$RESTARTXINETD
	echo "$XINETD $SUCCESS" >> $LOG
else
	echo "$XINETD is running fine at $(date)" >> $LOG
fi;
#
# Qmail
#
if
	[ $? -ne 0 ]
then
	$RESTARTQMAIL
	echo "$QMAIL $SUCCESS" >> $LOG
else
	echo "$QMAIL is running fine at $(date)" >> $LOG
fi;

Then if you want it to run every 5 or so minutes (I have mine on every 5 minutes), just add it to crontab. It’s an extremely light script and uses basically zero system resources.

That will keep your server running pretty smooth. If you want to get an email notification when this happens, it’s also fairly easy to set up. It would also be a good idea to check the logs once in a while and make sure you’re not having too many service issues.

Installing Eclipse to any Gnome Based Linux Distro

Problem – Eclipse is outdated in your repository, or else it’s not even in your repository.

Solution – install it from their site!

I have done this process in both RPM based and APT based distros and it works just fine. First thing you need to know though is that you need a JDK or JRE to run Eclipse. I personally have done it on CentOS (Stella), Fedora, Debian, Ubuntu (either Unity or Gnome), Mint (Mate, Cinnamon), etc… For some reason, I prefer Oracle’s JDK / JRE over OpenJDK / OpenJRE, but that is just a personal preference. I will write another article here in the future that tells how to install Oracles JDK. It tends to use less memory in my personal opinion, but that is pointless because I usually expand the memory that Eclipse can use.

I have never tried this with KDE, LXDE, or XFCE – so I’m not sure if it will work or not. I know it works on MATE, Unity, Cinnamon, and GNOME 2 and 3.

Here we go.

First you need to download the latest Eclipse (at the present it’s Juno) from http://www.eclipse.org. Choose Linux and choose your architecture (x86 or x64).

Next, open a terminal. This is where the fun starts. First you need to go to wherever your your downloads are. For most people this is in /home/user/Downloads or ~/Downloads

$ cd ~/Downloads

Next we need to untar Eclipse.

$ tar xvf eclipse*.tar.gz

This will create a folder in that folder called “eclipse”. Next we need to do this in this order. Become root first, or the rest will fail.

$ su
# mv eclipse /opt
chown -R root:root /opt/eclipse
chmod -R +r /opt/eclipse
touch /usr/bin/eclipse
chmod 755 /usr/bin/eclipse

Now we need edit /usr/bin/eclipse.

Use your favorite text editor  as super user (nano, vi, gedit, whatever):

# sudo gedit /usr/bin/eclipse

To punch this in to /usr/bin/eclipse

#!/bin/sh
export ECLIPSE_HOME="/opt/eclipse"
$ECLIPSE_HOME/eclipse $*

Next we need to make an application icon and launcher for it in the gnome menu. Use your favorite text editor to create /usr/share/applications/eclipse.desktop

# sudo gedit /usr/share/applications/eclipse.desktop

Add this to that file:

[Desktop Entry]
Encoding=UTF-8
Name=Eclipse
Comment=Eclipse IDE
Exec=/usr/bin/eclipse
Icon=/opt/eclipse/icon.xpm
Terminal=false
Type=Application
Categories=GNOME;Application;Development;
StartupNotify=true

And there you go. You can now launch Eclipse from the “Programming” section of Gnome 2 – if you are running Mate, Cinnamon, or Gnome 3, just search for Eclipse.

Issues:

I have had an issue in the past where no repositories will show up in Eclipse. You’ll have to manually add the latest repository – Name it Juno and add the link http://download.eclipse.org/releases/juno – then it will work for you.

I’ve also sometimes had an issue where the launcher we created at /usr/bin/eclipse doesn’t work. If that is your case, in eclipse.desktop, change the Exec= line to this:

Exec=/opt/eclipse/eclipse

And that should work for you.