Executes commands and send output to the file descriptor (fd)
From Linux Shell Scripting Tutorial - A Beginner's handbook
The syntax is as follows to run or execute commands and send output to the file descriptor:
command-name >& fd ./shell-script >& fd
For example, send output of 'free -m' command to the fd # 4:
#!/bin/bash exec 4> /tmp/out.txt free -m >&4
Shell Script To Collect System Information
Create a shell script called sysinfo.sh:
#!/bin/bash # get date in dd-mm-yyyy format NOW=$(date +"%d-%m-%Y") # create output file name OUTPUT="/tmp/sysinfo.$NOW.log" # Assign the fd 3 to $OUTPUT file exec 3> $OUTPUT # Write date, time and hostname echo "---------------------------------------------------" >&3 echo "System Info run @ $(date) for $(hostname)" >&3 echo "---------------------------------------------------" >&3 echo "****************************" >&3 echo "*** Installed Hard Disks ***" >&3 echo "****************************" >&3 fdisk -l | egrep "^Disk /dev" >&3 echo "************************************" >&3 echo "*** File System Disk Space Usage ***" >&3 echo "************************************" >&3 df -H >&3 echo "***********************" >&3 echo "*** CPU Information ***" >&3 echo "***********************" >&3 grep 'model name' /proc/cpuinfo | uniq | awk -F: '{ print $2}' >&3 echo "*****************************" >&3 echo "*** Operating System Info ***" >&3 echo "*****************************" >&3 uname -a >&3 [ -x /usr/bin/lsb_release ] && /usr/bin/lsb_release -a >&3 || echo "/usr/bin/lsb_release not found." >&3 echo "**************************************" >&3 echo "*** Amount Of Free And Used Memory ***" >&3 echo "**************************************" >&3 free -m >&3 echo "************************************" >&3 echo "*** Top 10 Memory Eating Process ***" >&3 echo "************************************" >&3 ps -auxf | sort -nr -k 4 | head -10 >&3 echo "**********************************" >&3 echo "*** Top 10 CPU Eating Process ***" >&3 echo "**********************************" >&3 ps -auxf | sort -nr -k 3 | head -10 >&3 echo "******************************************" >&3 echo "*** Network Device Information [eth0] ***" >&3 echo "******************************************" >&3 netstat -i | grep -q eth0 && /sbin/ifconfig eth0 >&3 || echo "eth0 is not installed" >&3 echo "******************************************" >&3 echo "*** Network Device Information [eth1] ***" >&3 echo "******************************************" >&3 netstat -i | grep -q eth1 && /sbin/ifconfig eth1 >&3 || echo "eth1 is not installed" >&3 echo "********************************" >&3 echo "*** Wireless Device [wlan0] ***" >&3 echo "********************************" >&3 netstat -i | grep -q wlan0 && /sbin/ifconfig wlan0 >&3 || echo "wlan0 is not installed" >&3 echo "*************************************" >&3 echo "*** All Network Interfaces Stats ***" >&3 echo "*************************************" >&3 netstat -i >&3 echo "System info wrote to $OUTPUT file."
Save and close the file. Run it as follows:
chmod +x sysinfo.sh ./sysinfo.sh
Sample outputs:
---------------------------------------------------
System Info run @ Sun Sep 20 02:41:43 IST 2009 for vivek-desktop
---------------------------------------------------
****************************
*** Installed Hard Disks ***
****************************
Disk /dev/sda: 1500.3 GB, 1500301910016 bytes
Disk /dev/sdb: 500.1 GB, 500107862016 bytes
Disk /dev/sdc: 500.1 GB, 500107862016 bytes
************************************
*** File System Disk Space Usage ***
************************************
Filesystem Size Used Avail Use% Mounted on
/dev/sdb2 99G 29G 65G 31% /
tmpfs 4.3G 0 4.3G 0% /lib/init/rw
varrun 4.3G 267k 4.3G 1% /var/run
varlock 4.3G 0 4.3G 0% /var/lock
udev 4.3G 2.9M 4.3G 1% /dev
tmpfs 4.3G 361k 4.3G 1% /dev/shm
/dev/sdb5 294G 282G 12G 96% /share
/dev/sdc2 247G 164G 71G 70% /disk1p2
/dev/sda5 1.4T 444G 861G 34% /1.5
***********************
*** CPU Information ***
***********************
Filesystem Size Used Avail Use% Mounted on
/dev/sdb2 99G 29G 65G 31% /
tmpfs 4.3G 0 4.3G 0% /lib/init/rw
varrun 4.3G 267k 4.3G 1% /var/run
varlock 4.3G 0 4.3G 0% /var/lock
udev 4.3G 2.9M 4.3G 1% /dev
tmpfs 4.3G 361k 4.3G 1% /dev/shm
/dev/sdb5 294G 282G 12G 96% /share
/dev/sdc2 247G 164G 71G 70% /disk1p2
/dev/sda5 1.4T 444G 861G 34% /1.5
*****************************
*** Operating System Info ***
*****************************
Linux vivek-desktop 2.6.27-11-server #1 SMP Wed Apr 1 21:53:55 UTC 2009 i686 GNU/Linux
Distributor ID: Ubuntu
Description: Ubuntu 8.10
Release: 8.10
Codename: intrepid
**************************************
*** Amount Of Free And Used Memory ***
**************************************
total used free shared buffers cached
Mem: 8105 4178 3926 0 181 3093
-/+ buffers/cache: 903 7201
Swap: 1906 0 1906
************************************
*** Top 10 Memory Eating Process ***
************************************
vivek 8413 5.8 4.1 658020 343488 ? Rl Sep19 29:51 \_ /opt/firefox/firefox-bin
vivek 18058 1.2 2.3 241724 198904 pts/1 Sl 00:54 1:18 vi /home/vivek/output.txt
vivek 8600 3.9 1.1 175616 93900 ? Sl Sep19 20:00 /usr/bin/python /usr/bin/deluge
root 7701 3.0 0.5 314200 41940 tty7 SLs+ Sep19 15:20 \_ /usr/X11R6/bin/X :0 -br -audit 0 -auth /var/lib/gdm/:0.Xauth -nolisten tcp vt7
tomcat55 7875 0.0 0.4 293688 36460 ? Sl Sep19 0:17 \_ /usr/bin/jsvc -user tomcat55 -cp /usr/share/java/commons-daemon.jar:/usr/share/tomcat5.5/bin/bootstrap.jar -outfile SYSLOG -errfile SYSLOG -pidfile /var/run/tomcat5.5.pid -Djava.awt.headless=true -Xmx128M -Djava.endorsed.dirs=/usr/share/tomcat5.5/common/endorsed -Dcatalina.base=/var/lib/tomcat5.5 -Dcatalina.home=/usr/share/tomcat5.5 -Djava.io.tmpdir=/var/lib/tomcat5.5/temp -Djava.security.manager -Djava.security.policy=/var/lib/tomcat5.5/conf/catalina.policy -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/var/lib/tomcat5.5/conf/logging.properties org.apache.catalina.startup.Bootstrap
vivek 8283 0.0 0.3 113548 28888 ? Sl Sep19 0:03 gnome-terminal
vivek 18341 0.3 0.3 62048 28732 pts/2 S 01:07 0:20 | \_ gedit /tmp/x
vivek 17561 0.0 0.3 77692 27172 ? S 00:33 0:01 \_ nautilus --no-desktop --browser
vivek 8181 0.0 0.2 52844 22180 ? S Sep19 0:15 \_ gnome-panel
vivek 8147 0.0 0.2 63868 17652 ? Ssl Sep19 0:02 /usr/lib/gnome-settings-daemon/gnome-settings-daemon
**********************************
*** Top 10 CPU Eating Process ***
**********************************
vivek 8413 5.8 4.1 658020 343836 ? Dl Sep19 29:51 \_ /opt/firefox/firefox-bin
vivek 8600 3.9 1.1 175616 93900 ? Sl Sep19 20:00 /usr/bin/python /usr/bin/deluge
root 7701 3.0 0.5 314200 41940 tty7 SLs+ Sep19 15:20 \_ /usr/X11R6/bin/X :0 -br -audit 0 -auth /var/lib/gdm/:0.Xauth -nolisten tcp vt7
vivek 8097 1.4 0.0 31124 4776 ? Ssl Sep19 7:18 /usr/bin/pulseaudio -D --log-target=syslog
vivek 18058 1.2 2.3 241724 198904 pts/1 Sl 00:54 1:18 vi /tmp/sysinfo.sh
vivek 18341 0.3 0.3 62048 28732 pts/2 R 01:07 0:20 | \_ gedit /tmp/x
root 8302 0.1 0.0 0 0 ? S< Sep19 0:53 \_ [ntos_wq]
www-data 8064 0.0 0.0 30204 2412 ? S Sep19 0:00 \_ /usr/bin/php-cgi
www-data 8063 0.0 0.0 30204 2412 ? S Sep19 0:00 \_ /usr/bin/php-cgi
www-data 8062 0.0 0.0 30204 2412 ? S Sep19 0:00 \_ /usr/bin/php-cgi
******************************************
*** Network Device Information [eth0] ***
******************************************
eth0 is not installed
******************************************
*** Network Device Information [eth1] ***
******************************************
eth1 is not installed
********************************
*** Wireless Device [wlan0] ***
********************************
wlan0 Link encap:Ethernet HWaddr 00:1e:2a:47:42:8d
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::21e:2aff:fe47:428d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:648676 errors:0 dropped:0 overruns:0 frame:0
TX packets:622282 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:465564933 (465.5 MB) TX bytes:310468013 (310.4 MB)
Interrupt:18 Memory:e3000000-e3010000
*************************************
*** All Network Interfaces Stats ***
*************************************
Kernel Interface table
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
lo 16436 0 6784 0 0 0 6784 0 0 0 LRU
vmnet1 1500 0 0 0 0 0 60 0 0 0 BMRU
vmnet3 1500 0 0 0 0 0 60 0 0 0 BMRU
vmnet8 1500 0 0 0 0 0 60 0 0 0 BMRU
wlan0 1500 0 648676 0 0 0 622282 0 0 0 BMRU