You are not logged in.

#1 2015-02-08 03:37:11

FoxzTrot
Member
Registered: 2014-02-20
Posts: 20

[Solved] Date command not working, maxing out memory and cpu.

So I've encountered and incredibly strange problem.

Today, I was working on a script for my bar panel. I was following z3bra's guide "Meeting at The Bar Really simple stuff, nothing harmful:

#! /bin/sh
#
# Fox - do whatever
# Gathers various info to be displayed on a panel using BAR

clock () {
	date '+%a %y/%m/%d %I:%M'
}

volume () {
	sed -n 's/^.*\[\([0-9]\+\)%.*$/\1%/p' 
}

cpuload () {
	mpstat 1 1 | awk '$3 ~ /CPU/ { for(i=1;i<=NF;i++) { if ($i ~ /%idle/) field=i } } $3 ~ /all/ { print 100 - $field }'  
}

memused() {
	 read t f <<< `grep -E 'Mem(Total|Free)' /proc/meminfo |awk '{print $2}'`
	 bc <<< "scale=2; 100 - $f / $t * 100" | cut -d. -f1
}

network() {
	#The Following assumes you have 3 interfaces: loopback, ethernet, wifi
	read lo int1 int2 <<< `ip link | sed -n 's/^[0-9]: \(.*\):.*$/\1/p'`

	# iwconfig returns an error code if the interface tested has no wireless
	# extensions
	if iwconfig $int1 >/dev/null 2>&1; then
		wifi=$int1
		eth0=$int2
	else 
		wifi=$int2
		eth0=$int1
	fi

	# in case you have only one interface, just set it here:
	# int=eth0

	# this line will set the variable $int to $eth0 if it's up, and $wifi
	# otherwise. I assume that if ethernet is UP, then it has priority over
	# wifi. If you have a better idea, please share :)
	ip link show $eth0 | grep 'state UP' >/dev/null && int=$eth0 || int=$wifi
	
	#This is now the time to see if network is up or not. For that, a simple ping would do the trick:

	# just output the interface name. Could obviously be done in the 'ping'
	# query
	echo -n "$int"

	# Send a single packet, to speed up the test. I use google's DNS 8.8.8.8,
	# but feel free to use any ip address you want. Be sure to put an IP, not a
	# domain name. You'll bypass the DNS resolution that can take some precious
	# miliseconds ;)
	ping -c 1 8.8.8.8 >/dev/null 2>&1 && echo "connected" || echo "disconnected"
}

groups() {
	cur=`xprop -root _NET_CURRENT_DESKTOP | awk '{print $3+1}'`
	tot=`xprop -root _NET_NUMBER_OF_DESKTOPS | awk '{print $3}'`

	# Desktop numbers start at 0. if you want desktop 2 to be in second place,
	# start counting from 1 instead of 0. But wou'll lose a group
	for w in `seq 1 $((cur - 1))`; do line="${line}="; done

	# enough =, let's print the current desktop
	line="${line}|"

	# En then the other groups
	for w in `seq $((cur + 2)) $tot`; do line="${line}="; done

	# don't forget to print that line!
	if (($cur < "10"))
		then echo $line=
		else echo $line
	fi
}

nowplaying() {
	cur=`mpc current`
	test -n "$cur" && echo $cur || echo "- stopped -"
}

# This loop will fill a buffer with our infos, and output it to stdout.
while :; do
	buf=""
	buf="${buf} [$(groups)]   --  "
	buf="${buf} CLK: $(clock) -"
	buf="${buf} NET: $(network) -"
	buf="${buf} CPU: $(cpuload)%% -"
	buf="${buf} RAM: $(memused)%% -"
	buf="${buf} VOL: $(volume)%%"
	buf="${buf} MPD: $(nowplaying)"

	echo $buf
	# use `nowplaying scroll` to get a scrolling output!
	sleep 1 # The HUD will be updated every second
done

Once I finished it up, I ran it, and nothing happened. So I took a break, and then came back to troubleshoot it. I ran

bash -x panel.sh

and didn't see anything weird so I started to just run each command and make sure my syntax was alright. However, right away when I ran date, no output was generated and within seconds my computer came to a crawl, so I rebooted manually.

Now, whenever I simply run (without any parameters)

date

the exact same thing happens and within seconds htop reports 100% memory usage and nearly 100% CPU usage on all four cores. Luckily, if I'm fast enough, ctrl-c still ends the operation of date.

I've tried reinstalling coreutils,but that doesn't seem to fix the problem and I can't completely remove it first since a lot of things depend on it.. I have no clue how to even start fixing this. Anyone have an idea?

Realized where I went stupid, in my ~/Scripts folder, I made a script named date without remember that I had recently added that folder to my PATH. So I created a recursive loop that within seconds would crash my computer... oops.

Last edited by FoxzTrot (2015-02-08 04:00:24)

Offline

Board footer

Powered by FluxBB