Archive for the 'bash' Category

Find out where Unix/Linux executable binary is located

There are two commands that may help you to find where executable binary is located regardless it’s Unix or Linux system. They are whereis and type. First locates source/binary and manuals sections for specified files and second tells what exactly shell executes when you type a certain command.

The next picture shows examples of these commands work.

Find out where Unix/Linux executable binary is located

Advertisements

One more way to count CPUs in Linux

Here is one more way to find out how many CPUs are used by Linux:
cat /proc/cpuinfo | grep “^vendor_id” | wc -l

As for me I use this way to make sure that SMP kernel should be installed.

You’re welcome to suggest your ways of doing the same in comments. Thank you in advance!

Automatic "zombie" processes killing (shell script)

Here is a shell script example that can be run by cron for automatic killing of “zombie” processes in Linux system:

#!/bin/bash
# Zombie processes killing script.
# Must be run under root.
case "$1" in
--admin)
        stat=`ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print"pid: "$3" *** parent_pid: "$4" *** status: "$10" *** process: "$13}' | grep ": Z"`

        if ((${#stat} > 0));then
    	    echo zombie processes found:
	    echo .
	    ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print"pid: "$3" *** parent_pid: "$4" *** status: "$10" *** process: "$13}' | grep ": Z"
	    echo -n "Kill zombies? [y/n]: "
	    read keyb
	    if [ $keyb == 'y' ];then
		echo killing zombies..
		ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print$4" status:"$10}' | grep "status:Z" | awk '{print $1}' | xargs -n 1 kill -9
	    fi
	else
	    echo no zombies found!
	fi
;;
--cron)
	stat=`ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print"pid: "$3" *** parent_pid: "$4" *** status: "$10" *** process: "$13}' | grep ": Z"`
        if ((${#stat} > 0));then
        ps ax | awk '{print $1}' | grep -v "PID" | xargs -n 1 ps lOp | grep -v "UID" | awk '{print$4" status:"$10}' | grep "status:Z" | awk '{print $1}' | xargs -n 1 kill -9
	echo `date`": killed some zombie proceses!" >> /var/log/zombies.log
	fi
;;
*)	echo 'usage: zombies {--cron|--admin}'
;;
esac
exit 0

Quick shell change for user in Unix or Linux

In order to change shell for a particular user of Unix/Linux system without editing /etc/passwd file manually just use command:

chsh -s /path/to/shell username

for example, to change shell to ‘sh’ from ‘bash’ for user ‘viper’, use command:

chsh -s /bin/sh viper

Encryption files with GPG without user intervention (batch)

I usually encrypt files with GPG with symmetric algorithms when I have to transmit files over insecure channel: for example I encrypt backup files stored at my USB stick and I’m sure that if it’s stolen or lost my files are in safety.

Encryption without user intervention can be used when you wish to perform automatic (runs with cron) backup procedure and protect backup files.

To encrypt file /tmp/file.tgz by symmetric AES algorithm (256 is a key lenght) with one command please use the following command:

echo password | gpg --batch -q --passphrase-fd 0 --cipher-algo AES256 -c /tmp/file.tgz

Previous command will create file.tgz.gpg that can be extracted automatically by next one:

echo password | gpg --batch -q -o /tmp/file.tgz --passphrase-fd 0 --decrypt /tmp/file.tgz.gpg

Make linux shell scripts encrypted

One day it came to my mind that bash scripts I was developing for a long time (approximately 5000 lines) should be encrypted in order not to be viewed by everyone…

Thanks to heaven there is utility written by Francisco Javier Rosales García SHC that allows to create a stripped binary executable version of the script.

This utility is included into many Linux distributions’ repositories (I’ve used it in Ubuntu and Fedora). To use it in Ubuntu (my laptop operating system) just install it by commands:

apt-get install shc
apt-get install build-essential

The second command installs dependencies for SHC. On my Ubuntu Feisty they weren’t installed automatically during SHC installation and I’ve got errors when compile scripts with SHC (that’s why I’ve installed them explicitly).

When necessary packages are installed compile some script with command:

shc -f script.sh

This command will create script.sh.x which can be run as a regular binary and is encrypted. Moreover this executable can be started only on the machine it’s compiled at (of course until you’ve compiled it with ‘relax security’ option -r).

This utility is also useful to create demo version binaries: it’s possible to set expiration date for the binary.

Respect to the author!


Archive