overall handy linux commands and scripts

get process properties status and uptime
ps -eo pid,comm,cmd,start,etime | grep -i apache

List all packages with version numbers in debian ubuntu
dpkg-query -W -f '${status} ${package} ${version}\n' | sed -n 's/^install ok installed //p' > installed-pkgs

Check OS x86 or x64
root@lxserver:~# getconf LONG_BIT
64
root@lxserver:~# uname -a
Linux lxserver 3.19.0-59-generic #66~14.04.1-Ubuntu SMP Fri May 13 17:27:10 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
root@lxserver:~#

find and rename directories -files-rights:
find . -depth -name '20150531'  -type d -execdir mv {} 20160531 \;
find . -type d -exec chmod 0755 {} \;
find . -type f -exec chmod 0644 {} \;

 find tekst in files
grep -Hrn 'search term' path/to/files

find /path -type f -exec grep -l -name "string" {} \;

grep --include=\*.php -rnw -e "Leave a comment"

replace tekst in files
find ./ -type f -exec sed -i 's/Leave a comment/Geef een reactie/' {} \;


Scripts:

Example: Check if apache process only runs one if not kill all apache proccess

#!/bin/bash
program_name=apache2
let max_instances=1
if ((count=$(ps -eocomm | grep -c "^$program_name$")> max_instances )); then
    killall "$program_name"
    echo $(date +%Y-%m-%d)":"$(date +%H:%M:%S)" Found $count $program_name processes. Killed."
fi
exit

Check if apache process is running, if not start service apache2

#!/bin/sh
process=ps auxwww | grep apache2 | grep -v grep | awk '{print $1}'`
if [ -z "$process" ]; then
echo $(date +%Y-%m-%d) $(date +%H:%M) "Couldn't find apache2 running. Restarting server-binary" >> /var/apache2log/apache2.check
echo && date >>/var/apache2log/apache2.check
service apache2 start >> /var/apache2log/apache2.log &
else echo $(date +%Y-%m-%d) $(date +%H:%M) "apache2 is OK!" >> /var/apache2log/apache2.check
fi

No Comments

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *