Raspberry Pi Refresh Browser - bash

I have a raspberry pi set up to act as a signage system using a google presentation url as what its displaying. Problem is I need this page to be refreshed every 5 minutes to grab new information added or removed from the slideshow.
What I had set up was a small cron job running every 5 minutes
*/5 * * * * export DISPLAY=:0 && /bin/bash /home/pi/refresh.sh
[xdotool search “Chromium” windowactivate --sync key F5] <- Contents of refresh.sh
However as I have noticed from watching the display it is not auto refreshing and if I run that command manually from ssh it refreshes just fine.
Does anybody have any tips? Maybe I'm missing something?

I actually got it to work by modifying the refresh.sh with
sudo -u pi screen -d -m export DISPLAY=:0 && xdotool search "Chromium" windowactivate --sync key F5 > /dev/null 2>&1
I guess the cronjob runs under a separate shell that when it tried to fetch the display with xdotool it wasn't able to. With this the cron job actually fires up a shell as the pi user and then executes the xdotool which then runs the command like it should and then once that command is done the screen session dies. Effectively doing exactly what I needed for the digital sign.

Related

Check if shell script is already running (feh) (i3)

Inside my ~/Pictures/wallpapers/ folder I have a shell script that changes the wallpaper using a while true each time the sleep function terminates:
feh-auto.sh
#!/bin/bash
while true; do
feh /home/maruko/Pictures/wallpapers --randomize --bg-fill
sleep 1800
done
this script is automatically run each time the laptop powers up, or the i3 configuration is reloaded in place using the shortcut Win+Shift+R:
~/.config/i3/config
...
### CHANGE WALLPAPER RANDOMLY
# https://www.linuxandubuntu.com/home/set-background-wallpapers-on-i3wm
exec_always --no-startup-id ~/Pictures/wallpapers/feh-auto.sh
...
Making changes to the i3 configuration is usually accompanied with a reload in place, so that said changes take place.
The problem is that each time a reload occurs, a new instance of feh-auto.sh is also created, meaning that now instead of 1 timer, I have multiple timers of 1800, that will change the wallpaper x times where x is the number of times I reloaded the configuration.
I would like a more appropriate behavior as I reload: check if feh-auto.sh is already running and if it is, do not create a new instance.
Could you please guide me to the best solution?
Thank you.
EDIT
As #balki suggested in the comments, I have created a cron job
(sudo crontab -e)
#reboot /home/maruko/Documents/program-files/shell/feh-auto.sh
to run the following script
feh-auto.sh
#!/bin/bash
export DISPLAY=0:0
feh /home/maruko/Pictures/wallpapers/ --randomize --bg-fill
However, when booting up or rebooting the system, the wallpaper is blank as if nothing really happened. The command sudo systemctl status cronie.service reports the following:
crond[3300]: pam_unix(crond:session): session opened for user root(uid=0) by (uid=0)
CROND[3307]: (root) CMD (/home/maruko/Documents/program-files/shell/feh-auto.sh)
CROND[3300]: (root) CMDOUT (feh ERROR: Can't open X display. It *is* running, yeah?)
CROND[3300]: (root) CMDEND (/home/maruko/Documents/program-files/shell/feh-auto.sh)
CROND[3300]: pam_unix(crond:session): session closed for user root
I don't know what to do next.
The answer to this post is to read documentation:
i3 config comes with the exec and exec_always commands:
exec will execute the command once at boot,
exec_always will execute the command on reloads too.
The solution is to substitute the above call to feh-auto.sh:
~/.config/i3/config
### START FEH WALLPAPER
# note: exec_always will run the script on each reload, not ideal
# --no-startup-id will eliminate the problem of loading icon on boot
exec --no-startup-id /home/maruko/Pictures/wallpapers/feh-auto.sh

Run bash script after logon is made

I want to run a .sh script once whenever a logon is made (including logons made after a suspend)
It should run in the background, so no shell poping up
The script is the following
#!/bin/bash
# Bash script to refresh the Extend Panel Menu extension that puts the clock in the wrong place after logon
gnome-shell-extension-tool -d extend-panel-menu#julio641742
sleep 1
gnome-shell-extension-tool -e extend-panel-menu#julio641742
It just refreshes an extension that I have in Ubunto 18.04 that gets misconfigured in every logon I do.
How can I make it run automatically after those logons?

How to enter keyboard shortcuts in bash script

Is it possible to enter keyboard shortcuts into a bash script? For example, I have tried typing in ^d to substitute for control + d (logout) without success.
ubuntu#vfleet:~$ ^d
-bash: :s^d: substitution failed
ubuntu#vfleet:~$ ^D
-bash: :s^D: substitution failed
I am using screen to run a django development server in the background. One of the commands to run a particular screen as a daemon is control + a + d. The goal is to be able to enter in control + a + d into a bash script. For example:
python manage.py runserver
^a + d
Is this possible?
Edit:
A valid method of avoiding the keyboard shortcut in screen is linked by Eric Renouf in the comments below. screen -d -m sh -c "python manage.py runserver" will start a development server as a daemon. This is a great solution for my particular problem, but it would still be nice to have a solution for the original question at hand.
xdotool package is the solution here.
xdotool key ctrl+d
Full reference
You probably should just start the command without attaching to the screen session in the first place, like
screen -d -m python manage.py runserver
but if you can't do that for some reason, you could detach from a screen session you're currently in by doing:
screen -S "$STY" -X detach
screen saves its current session info in STY, so we'll use that to make sure we're interacting with the correct session (in case there are many). Then we'll use -X to send a command to that session, in this case our command will be detach which will detach all the attached sessions, including the one used to execute that command
So while this doesn't actually send key strokes, it does highlight that there is often another command that you can send to accomplish your goals. Here detach takes the place of ctrl+a+d. Sending quit or running exit could often replace ctrl+d.
Another work-around would be to use expect which you could then use to send the strings containing control characters or hex values of them.

Bash script executes fine on its own, but not with cron

I type
crontab -e
my crontab looks like
*/1 * * * * /home/sara/Desktop/kioskscripts/reloadpage.sh >> /home/sara/Desktop/kioskscripts/logfile.log
logfile is created in /kioskscripts but remains empty.
reloadpage.sh looks like this
#!/bin/bash
sleep 5
/usr/bin/xdotool key F5
sh reloadpage.sh
works as expected and simulates f5 being pressed 5 seconds after execution.
The program executed by cron does not have an active window, so you would need to explicitly specify which window you want the keystroke to be sent to using the --window option.
You can get the window id of your currently active window with xdotool getactivewindow and then use that number in an xdotool command. Or you can use xdotool search with various options to find the window you want to direct the keystroke to. Read man xdotool for the various search options. (You can do that in a single command: xdotool search --name Foo key F5 will send F5 to a window with FOO in its name.)
But that will only work if the indicated window accepts the events, and many windows don't.

Auto Restart SH script on crash?

Hi there guys i have a server running a game I've created and it has three SH scripts that are required to run in separate terminals so what i wanna know is 2 things.
1:is there a way i can get a single script that i double click on and launch all three scripts to where i can see the shell (for Debugging)
2: Is there any way to have said scripts auto restart when they exit or crash? (for full automated access when the server is unattended by a dev)
Server Specs:
6gb ram 60gb SSD 6 core CPU
Ubuntu 14.04
with vnc for desktop control
Here's a SH script for you.
running=1
finish()
{
running=0
}
trap finish SIGINT
while (( running )); do
// Execute the command here that starts your server.
echo "Restarting server on crash.."
sleep 5
done
You can run this script for each server in it's own screen. That way you can see the console output of each one. For example:
screen -S YOURUNIQUENAME -m THESCRIPTABOVE.sh
In order to detach from the screen, hit CTRL + A then CTRL + D. You can get back to the screen by using screen -x YOURUNIQUENAME
For a nice guide on using the screen command, see this article: http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/ . It even has a video to show how it's used.

Resources