Opening and closing Iceweasel for Kiosk - raspberry-pi

I'm trying to set a kiosk up that turns the HDMI port off at night and on weekends. I set up a crontab to turn it off but when it turns on the screen is black until some movement makes it load back. Becuase of this I am trying to make Iceweasel close and reopen fullscreen to load the screen. So far Iceweasel closes and the HDMI port turns off and on but I can't get Iceweasel to reopon. How can I do it?
Terminal
pi#raspberrypi:~$ crontab -e
#HDMI O/F
18 * * * tvservice -o > /dev/null && pkill iceweasel
8 * * 1-5 tvservice -p > /dev/null && fbset -depth 8 && fbset -depth 16 && $
2 8 * * 1-5 /home/pi/run_iceweasel.sh
run_iceweasel.sh
#!/bin/sh
/usr/bin/iceweasel
end 0

Related

Bash pipe vs input redirect vs process substitution performance

On one of my systems during processing some text files I noticed very strange thing. Somehow input redirect and process substitution are much slower than piping. Here is example with 1000 lines 137KB text file:
$ time cat 1000.log | while read -r line; do echo -n ''; done
real 0m0.159s
user 0m0.040s
sys 0m0.133s
$ time while read -r line; do echo -n ''; done < 1000.log
real 2m20.143s
user 0m55.205s
sys 1m44.233s
$ time while read -r line; do echo -n ''; done < <(cat 1000.log)
real 2m10.385s
user 0m49.853s
sys 1m38.208s
This is crazy ~88000% difference!
Another weird example:
$ time for i in {1..100}; do echo $i; done
1
2
3
...
99
100
real 0m6.773s
user 0m5.372s
sys 0m2.424s
On other hosts it takes miliseconds...
What may be wrong with that exact system (SUSE Linux Enterprise Server 12 SP2)?

exit fullscreen OMX Player

I started a omx instance based on this script. it works great, but I can't quit the player with q or alt f4 or ctrl-z.in desktop the keyboard commands always arrive on the desktop. especially if I start the script on the raspberry in CLI mode, I would never get out of the video.
What can I do here?
#!/bin/bash
### BEGIN INIT INFO
# Provides: omxplayer
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Displays video file in a loop using omxplayer
# Description:
### END INIT INFO
# Video (replace with the path and file name of your video)
video_path=/home/pi/video.mp4
#---- There should be no need to edit anything below this line ----
# Start displaying the video loop
case "$1" in start)
screen -dmS videoloop sh -c "omxplayer -o both $video_path -b --loop --no-osd"
echo "Video Playback Started"
;;
# Stop displaying video loop
stop)
sudo killall omxplayer.bin
echo "Video Playback Stopped"
;;
# Restart video loop if it died
repair)
if !(ps -a | grep omx -q)
then
screen -dmS videoloop sh -c "omxplayer -o local $video_path -b --loop --no-osd"
echo "The Video is now running"
fi
;;
*)
echo "Usage: /etc/init.d/videoloop {start|stop|repair}"
exit 1
;;
esac

How to replace date -d from script running on Linux to get it working on AIX

On a Linux system I'm running a bash script what is running fine, but now I want to use the same script on an AIX system.
Only a small part of it isn't able to run because on the AIX system the command "date -d" is not working.
Below is the code that is running on the linux server, baised on the hostname in this case system1 and system2 a script will run only on a Sunday at the given time.
declare -A myservertime
myservertime["system1"]="02:00"
myservertime["system2"]="02:10"
for keys in "${!myservertime[#]}";
do
if [[ "$keys" == "$HOSTNAME" ]]; then
mylaunchtime=${myservertime["$keys"]}
fi
done
if [ -z "$mylaunchtime" ]; then
echo "Server not found."
exit
fi
timenow=$(date +"%s")
weekday=$(date +"%a" -d #$timenow )
timerun=$(date +"%s" -d ${mylaunchtime} )
if [ $timerun -lt $timenow ]; then
timerun=$(( timerun + 86400 ))
fi
sleep_time=$(( timerun - timenow ))
#Check is weekday = Sunday
if [ $weekday == "Sun" ]; then
#If Sunday then wait until starttime is reached, until then sleep
sleep $sleep_time
#If starttime is reached execute startprogram
startprogram
fi
I already fixed the declare part, this is also not working on AIX, to the following:
typeset -A myservertime=(["pvm00066"]="02:30" ["pvm00100"]="01:30")
But I have troubles to get the variables $weekday and $timerun working, because of that "date -d $variable"
Anyone here that can help me with this, I tried and searched all kind of stuff but I was unable to get one working.
When you do want to have an identical crontab and script on all systems, and you only want to run the script on Sunday, just use an offset from 00:00.
Crontab:
0 0 * * 7 start_with_delay.sh
start_with_delay.sh:
declare -A myservertime
myserversleep["system1"]=7200
myserversleep["system2"]=7800
sleep ${myserversleep[HOSTNAME]}
startprogram
When you do not need fixed starting times for the different hosts, but only want different hosts to have different starting times, you can consider using the the last number of the IP as an input for calculating the sleep time.
Two reasons why you might consider another solution:
Starting a job with crontab, that starts with sleeping, is very confusing.
An operator reading the crontab expects something to start at the time given in the crontab.
Using a scriptname like sleep_and_start helps a little.
When the system reboots at Sunday 01:00, nothing is started at 02:00
The sleep command has been stopped during the reboot.
I would try to find a smart install script, that will write a different crontab on each host. Something with Ansible, a Puppet template or shellscript.
case $HOSTNAME in
system1)
starthour=2
startminute=0
;;
system2)
starthour=2
startminute=10
;;
*)
echo "No scheduled startprogram on ${HOSTNAME}.
exit 0
;;
esac
echo "${startminute} ${starthour} * * SUN startprogram" | crontab -
When you want to update en existing crontab, you might want something like
(crontab -l 2>/dev/null && echo "${startminute} ${starthour} * * 7 startprogram") |
sort -u | crontab -

Open File keeps growing despite emptied content

How can I pipe a text stream into a file and, while the file is still in use, wipe it for job rotation?
Long version:
I've been struggling for a while onto an apparently minor issue, that's making my experiments impossible to continue.
I have a software collecting data continuously from external hardware (radiotelescope project) and storing in a csv format. Being the installation at a remote location I would, once a day, copy the saved data in a secure place and wipe the file content while, for the same reason, I can NOT to stop the hardware/software, thus software such as log rotation wouldn't be an option.
For as much effort spent see my previous post, it seems the wiped file keeps growing although empty.
Bizarre behavior, showing file size, truncate file, show file size again:
pi#tower /media/data $ ls -la radio.csv ;ls -s radio.csv;truncate radio.csv -s 1; ls -la radio.csv ;ls -s radio.csv
-rw-r--r-- 1 pi pi 994277 Jan 18 21:32 radio.csv
252 radio.csv
-rw-r--r-- 1 pi pi 1 Jan 18 21:32 radio.csv
0 radio.csv
Then, as soon as more data comes in:
pi#tower /media/data $ ls -la radio.csv ;ls -s radio.csv
-rw-r--r-- 1 pi pi 1011130 Jan 18 21:32 radio.csv
24 radio.csv
I thought to pipe the output into a sed command and save right away, with no luck altogether. Also, filesystem/hardware doesn't seems buggy (tried different hardware/distro/filesystem).
Would anyone be so nice to give me a hint how to proceed?
Thank you in advance.
Piped into tee with -a option. The file was kept open by originating source.
Option APPEND of tee helped to stick at the EOF new data; in the given issue, appending to the beginning when file zeroed.
For search engine and future reference:
sudo rtl_power -f 88M:108M:10k -g 1 - | tee -a radio.csv -
Now emptying the file with
echo -n > radio.csv
gets the file zeroed as expected.

Get current playing file in MPlayer slave mode

Problem: I can't find any way to reliably get the current playing file in an MPlayer playlist.
Here is how far I have gotten. This working ash script monitors a text file with the path to the current playlist. When I update the file, the script closes the old instance of MPlayer and opens a new one with the new playlist:
# POLL PLAYLIST FILE FOR CHANGES
CURRENTPLAYLISTPATH=/home/tc/currentplaylist
INFIFO=/tmp/mplayer-in
CURRENTPLAYLIST="NEVERMATCHAPLAYLIST"
FIRSTRUN=1
while [ 1 ];
do
# CHECK FOR NEW PLAYLIST
NEWPLAYLIST=$(head -n 1 $CURRENTPLAYLISTPATH)
if [[ "$NEWPLAYLIST" != "$CURRENTPLAYLIST" ]]; then
if [ "$FIRSTRUN" == 0 ]; then
echo "quit" > "$INFIFO"
fi
# CREATE NAMED PIPE, IF NEEDED
trap "rm -f $INFIFO" EXIT
if [ ! -p $INFIFO ]; then
mkfifo $INFIFO
fi
# START MPLAYER
mplayer -fixed-vo -nolirc -vc ffmpeg12vdpau,ffh264vdpau, -playlist $NEWPLAYLIST -loop 0 -geometry 1696x954 -slave -idle -input file=$INFIFO -quiet -msglevel all=0 -identify | tee -a /home/tc/mplayer.log &
CURRENTPLAYLIST=$NEWPLAYLIST
FIRSTRUN=0
fi
sleep 5;
done
My original plan was just to use the "-identify" flag and parse the log file. This actually works really well up until I need to truncate the log file to keep it from getting too large. As soon as my truncating script is run, MPlayer stops writing to the log file:
FILENAME=/home/tc/mplayer.log
MAXCOUNT=100
if [ -f "$FILENAME" ]; then
LINECOUNT=`wc -l "$FILENAME" | awk '{print $1}'`
if [ "$LINECOUNT" -gt "$MAXCOUNT" ]; then
REMOVECOUNT=`expr $LINECOUNT - $MAXCOUNT`
sed -i 1,"$REMOVECOUNT"d "$FILENAME"
fi
fi
I have searched and searched but have been unable to find any other way of getting the current playing file that works.
I have tried piping the output to another named pipe and then monitoring it, but only works for a few seconds, then MPlayer completely freezes.
I have also tried using bash (instead of ash) and piping the output to a function like the following, but get the same freezing problem:
function parseOutput()
{
while read LINE
do
echo "get_file_name" > /tmp/mplayer-in
if [[ "$LINE" == *ANS_FILENAME* ]]
then
echo ${LINE##ANS_FILENAME=} > "$CURRENTFILEPATH"
fi
sleep 1
done
}
# START MPLAYER
mplayer -fixed-vo -nolirc -vc ffmpeg12vdpau,ffh264vdpau, -playlist $NEWPLAYLIST -loop 0 -geometry 1696x954 -slave -idle -input file=/tmp/mplayer-in -quiet | parseOutput &
I suspect I am missing something very obvious here, so any help, ideas, points in the right direction would be greatly appreciated.
fodder
Alright then, so I'll post mine too.
Give this one a try (assuming there is only one instance running, like on fodder's machine):
basename "$(readlink /proc/$(pidof mplayer)/fd/* | grep -v '\(/dev/\|pipe:\|socket:\)')"
This is probably the safer way, since the file descriptors might not always be in the same order on all systems.
However, this can be shortened, with a little risk:
basename "$(readlink /proc/$(pidof mplayer)/fd/*)" | head -1
You might probably like to install this, too:
http://mplayer-tools.sourceforge.net/
Well, I gave up on getting the track from MPlayer itself.
My 'solution' is probably too hackish, but works for my needs since I know my machine will only ever have one instance of MPlayer running:
lsof -p $(pidof mplayer) | grep -o "/path/to/my/assets/.*"
If anyone has a better option I'm certainly still interested in doing this the right way, I just couldn't make any of the methods work.
fodder
You can use the run command.
Put this in ~/.mplayer/input.conf:
DEL run "echo ${filename} ${stream_pos} >> /home/knarf/out"
Now if you press the delete key while playing a file it will do what you expect i.e. append the current file playing and the position in the stream to the ~/out file. You can replace echo with your program.
See slave mod docs for more info (Ctrl-F somevar).
About getting properties from MPlayer
I have used a non-elegant solution, but it is working for me.
stdbuf -oL mplayer --slave --input=file=$FIFO awesome_awesome.mp3 |
{
while IFS= read -r line
do
if [[ "${line}" == ANS_* ]]; then
echo "${line#*=}" > ${line%=*} # echo property_value > property_name
fi
done
} &
mplayer_pid=&!
read filename < ./ANS_FILENAME
read timeLength < ./ANS_LENGTH
echo ($timeLength) $filename
and so on..
It is in another proccess, that's why I've used files to bring properties
'stdbuf' is for not to miss anything
I started putting together a bash library to handle tasks like this. Basically, you can accomplish this by dumping the mplayer output to a file. Then you grep that dump for "Playing " and take the last result with tail. This should give you the name of the file that's currently playing or that last finished playing.
Take a look at my bash code. You'll want to modify the playMediaFile function to your needs, but the getMediaFileName function should do exactly what you're asking. You'll find the code on my github.