How to keep a job running in Linux

There are many ways to keep a process running on linux but I haven’t seen any that are as easy to implement as the script below.

Basically the script does a ps ax and then a grep for your process. If it’s not running it will re-start the process.

You install the script into your crontab i.e. crontab -e

As a bonus this mechanism will re-start your process after a re-boot. Continue reading “How to keep a job running in Linux” »

Share

IP Setting on CentOS6 using Shell Script

#!/bin/bash

if [ $# -eq 5 ]
then

echo ""
echo "Taking the backup and Changing the hostname from $(hostname) to $1 ..."

sed -i.bk "s/$(hostname)/$1/g" /etc/sysconfig/network

echo ""
echo "Backing up & Assigning the Static IP ..."
echo ""

cp /etc/sysconfig/network-scripts/ifcfg-$2 /etc/sysconfig/network-scripts/$2.bk

cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-$2

DEVICE=$2
BOOTPROTO=static
IPADDR=$3.$4
NETMASK=255.255.255.0
GATEWAY=$3.$5
ONBOOT=yes
EOF

echo "Changing the dns ..."
echo ""

sed -i.bk "s/nameserver.*/nameserver $3.$5/" /etc/resolv.conf

echo "Adding $1 as hostname to the /etc/hosts file .."
echo ""

sed -i.bk "/$(hostname)$/d" /etc/hosts
echo "$3.$4 $1" >> /etc/hosts

echo "Restarting the Network Service, Please connect it using the new IP Address if you are using ssh ..."

service network restart

else

echo "Usage: ip.sh <hostname> <interface> <baseip> <ipaddress> <gateway/dns>"
echo "Example: ip.sh testname eth0 10.10.10 41 1"

fi
Share

Bash Shellshock fix with scripts for Debian, Ubuntu, CentOS and other distros. including old

First check if your Bash is vulnerable, execute the following command-

env x='() { :;}; echo vulnerable’ bash -c ‘echo this is a test’

If your system is vulnerable, you will see:

vulnerable

this is a test

If your system is not vulnerable, you will see:

bash: warning: x: ignoring function definition attempt

bash: error importing function definition for `x’

this is a test

To check for the vulnerability CVE-2014-6271, run the following in Bash:

env X='() { (a)=>\’ sh -c “echo date”; cat echo

If your system is vulnerable, you will see:

bash: X: line 1: syntax error near unexpected toke `=’

bash: X: line 1: `’

bash: error importing function for `X’

Sun Sep 08:17:32 EST 2014

If your system is not vulernable, you will see:

date

cat: echo: No such file or directory

To test the vulnerability CVE-2014-7186, run the following in Bash:

bash -c ‘true <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF <<EOF’ || echo “CVE-2014-7186 vulnerable, redir_stack”

If your system is not vulnerable, you will see:

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

bash: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)

So, let’s start fixing it!

 

Below, I’ve tried to put various contributors solution to place in a single entry.

For Debian 6 (Squeeze)

 

Append this to your sources.list:

deb http://http.debian.net/debian squeeze-lts main contrib non-free

deb-src http://http.debian.net/debian squeeze-lts main contrib non-free

and then run

apt-get update

apt-get install -t squeeze-lts –only-upgrade bash Continue reading “Bash Shellshock fix with scripts for Debian, Ubuntu, CentOS and other distros. including old” »

Share

CRON scripts to running in seconds interval

 

This problem can be solved with simple bash script. For example, if you need to run a PHP script on every 20 seconds, you can create a bash script like this:
#!/bin/bash
#Name:myscript.sh
#Desc:Run script in every 20 seconds
while (sleep 20 && php /path_to_your_script/your_script_name.php) &
do
wait $!
done
Then, make script executable, and add it to the system startup. That’s all.
Share

Send HTTP requests using cURL

 

Reading a URL via GET:
curl http://example.com/
Defining any HTTP method (like POST or PUT):
curl http://example.com/users/1 -XPUT
Sending data with a request:
curl http://example.com/users -d"first_name=Bruce&last_name=Wayne"
If you use -d and do not set an HTTP request method it automatically defaults to POST. Performing basic authentication:
curl http://user:password@example.com/users/1
All together now:
curl http://user:password@example.com/users/1 -XPUT -d"screen_name=batman"
Share