Running Shell Script after boot on Raspberry PI - linux

I'm making a web kiosk display board using a raspberry pi and I want to send some key strokes to the browser window 2 minutes after it's loaded. The script sends the logon details for a webserver.
I've got a script that sends the keystrokes which works fine from the telnet console:
#!/usr/bash
username="username"
password="password"
echo "Setting Display"
export DISPLAY=:0
echo "Sending Username"
for char in $(sed -E s/'(.)'/'\1 '/g <<<"$username"); do
xdotool key $char
done
xdotool key Tab
echo "Sending Password"
for char in $(sed -E s/'(.)'/'\1 '/g <<<"$password"); do
xdotool key $char
done
xdotool key Return
echo "Waiting 5 Seconds"
sleep 5
echo "Setting Remember Password"
xdotool key Tab
xdotool key Tab
xdotool key Return
echo "Finished"
I've tried to add bash /home/pi/logon.sh to the rc.local file - but it doesn't send the keystrokes to the browser?
Does any one know why that would be? As I say - it works fine from the telnet window if I run the script, but it doesn't work when run from boot.
I had sleep 120 on the line before it to stop if firing right away and wait until the browser has loaded - and I know the script is running from rc.local, because when I remove the sleep command, I see the echos from the script.
Any ideas?

The reason it wasn't working was because the script needed to be run as the user pi.
I changed the code in the rc.local script to this: su - pi -c "bash /home/pi/logon.sh &"
This makes the script run as the user pi and the ampersand is used to make the script run separate to the rc.local script by forking it. (http://hacktux.com/bash/ampersand)

Put this in your crontab
#reboot /path/to/script
Edit it using
#crontab -e

Related

Linux script not executing properly after a reboot

I am using a raspberry pi4 with 32bit Raspbian to display some webpages. I have a script to open 2 webpages, one on each monitor. The end of the script selects one of the pages and enters in login credentials. The script works great and I have it working with systemctl to auto start on reboot. The problem is the script doesn't always select the window after a reboot of my pi. It works 50% of the time. Which I believe is just which page happens to open first. Sometimes the cursor is left on the correct page. This happens whether I am using the systemctl or not.
The line it seems to ignore is:
xdotool search --name 'Login'>xdotool windowactivate
Troubleshooting that I did:
Waiting a long time after rebooting - This didn't work
Changed windowactivate to windowfocus - This made no difference
Removing the --kiosk flag - This didn't work
Closing the windows and running the script again - Works every subsequent time
Opening a browser and closing it after reboot then running script - Works every subsequent time
This led me to the next step which was to add a sleep in between opening the browsers. My hope was that once the second browser opened it would just be left as the active browser. Unfortunately this solidified my conclusion that the script isn't executing properly after a reboot.
After adding the sleep, the browsers open simultaneously after a reboot ignoring the sleep command. However, if I close the browsers and run the script again the sleep command works and 1 browser opens then the other one after the sleep.
Then I tried adding at the beginning of the script to open a browser and
then close it and then continuing the script -This didn't work.
Almost as if ignoring the opening and closing of the browser? I tried buffering the 2 commands with sleep but it ignored those as well.
The script works 100% of the time either the second time it is run or after opening and closing a browser.
Below is my script.
#!/bin/bash
xset s noblank
xset s off
xset -dpms
unclutter -idle 0.5 -root &
sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' /home/pi/.config/chromium/Default/Preferences
sed -i 's/"exit_type":"Crashed"/"exit_type":"Normal"/' /home/pi/.config/chromium/Default/Preferences
rm -rf ~/.config/chromium/Singleton*
/usr/bin/chromium-browser --new-window --noerrdialogs --disable-infobars --kiosk --user-data-dir="/tmp/1" --window-position=0,0 'http://ExampleDisplay.com' &
/usr/bin/chromium-browser --new-window --noerrdialogs --disable-infobars --kiosk --user-data-dir="/tmp/2" --window-position=2500,0 'http://ExampleLogin.com' &
sleep 7
xdotool search --name 'Login'>xdotool windowactivate
sleep 2.5
xdotool type 'TEST' # CHANGE THIS FOR DIFFERENT LOGIN
sleep .2
xdotool key 'Tab'
sleep .2
xdotool type 'TEST' # CHANGE THIS FOR DIFFERENT LOGIN
sleep .2
xdotool key 'Return'
sleep 4
xdotool type 'TESTORG' # CHANGE THIS FOR DIFFERENT ORGANIZATION
sleep .2
xdotool key 'Tab'
sleep .2
xdotool key 'Tab'
sleep .2
xdotool key 'Tab'
sleep .2
xdotool type 'TESTSTATION' # CHANGE THIS FOR DIFFERENT STATION
sleep .2
xdotool key 'Return'
To rule out other parts of the script I tried boiling it down to the very basic issue of opening the browsers one after the other with this script. This had the same issue: after a reboot it opens both browsers ignoring the sleep function. Then, if run again, it works.
#!/bin/bash
/usr/bin/chromium-browser --new-window --noerrdialogs --disable-infobars --kiosk --user-data dir="/tmp/1" --window-position=0,0 'http://ExampleDisplay.com' &
sleep 5
/usr/bin/chromium-browser --new-window --noerrdialogs --disable-infobars --kiosk --user-data-dir="/tmp/2" --window-position=2500,0 'http://ExampleLogin.com' &
Lastly, here is my systemctl (this shouldn't be needed because it happens regardless of using the systemctl, but just in case)
[Unit]
Description=Chromium Kiosk
Wants=graphical.target
After=graphical.target
[Service]
Environment=DISPLAY=:0.0
Environment=XAUTHORITY=/home/pi/.Xauthority
Type=forking
ExecStart=/bin/bash /home/pi/kiosk1.sh
Restart=on-abort
User=pi
Group=pi
[Install]
WantedBy=graphical.target
As a side note, I tried an alternative solution using a python script for the opening of the browsers using the multibrowse.py script from https://github.com/foxxyz/multibrowse and had the same issues even when adding the sleep calls between the browsers in the python script.
I don't use xdotool, but suspect you want:
xdotool windowactivate $(xdotool search --name 'Login')
Hopefully that means you will activate the window whose name is 'Login', which the xdotool search ... found.

How can we prevent CTRL-C from screen terminating?

I'm currently writing a bash script that would create multiple shell instances (with screen command) and execute a subprogram.
The problem is when I try to interrupt the subprogram, it interrupts the screen instance too. I already searched for trap command on internet with SIGINT but I don't really know how to use it in this case.
Here is my code to show you how do I create the screen:
#!/bin/bash
#ALL PATHS ARE DECLARED HERE.
declare -A PATHS; declare -a orders;
PATHS["proxy"]=/home/luna/proxy/HydraProxy; orders+=( "proxy" )
PATHS["bot"]=/home/luna/bot; orders+=( "bot" )
#LAUNCH SERVERS
SERVERS=/home/luna/servers
cd "$SERVERS"
for dir in */; do
d=$(basename "$dir")
PATHS["$d"]="$(realpath $dir)"; orders+=( "$d" )
done
for name in "${orders[#]}"; do
if ! screen -list | grep -q "$name"; then
path="${PATHS[$name]}"
cd "$path"
screen -dmS "$name" ./start.sh
echo "$name CREATED AT $path"
sleep 2
else
echo "SCREEN $name IS ALREADY CREATED"
fi
done
Could you help me more to find a solution please ? Thank you very much for your time.
Each of your screen instances is created to run a single command, start.sh. When this command terminates, for instance when you interrupt it, the screen will have done its job and terminate. The reason for this is that screen runs shell scripts directly in a non-interactive shell, rather than spawning a new interactive shell and running it there.
If you wanted to run start.sh inside an interactive shell in each screen, you'd do something like this:
screen -dmS "$name" /bin/bash -i
screen -S "$name" -X stuff "./start.sh^M"
The ^M is needed as it simulates pressing enter in your shell within screen.
If you use this, then when you interrupt a script within screen, you will still be left with an interactive prompt afterward to deal with as you see fit.

Output of bash script is going to another putty session

I have a script as below, when I am running this script in the 1st putty session. The output of script is displayed on the 1st putty session. If I open another (2nd) putty session and run the same script on 2nd putty session, still the output goes to the old putty (1st) session.
echo "*******Output command $args starts ***********"
pf=`oprmsg 'VARY CN(*),ACTIVATE'`
echo $pf
sleep 1
echo `oprmsg "$#"`
sleep 7
echo `oprmsg 'VARY CN(*),DEACTIVATE'`
sleep 1
echo "******* Output of command $args ends *************"
any idea why?

How can we open minicom from current terminal and pass multiple commands to execute and exit to 1st terminal using shell script

I am trying to write a shell script that creates a new window and run a minicom terminal (connected to /dev/ttyACM0) in that.
Here's the script file my_script.sh:
#!/bin/bash
gnome-terminal --command minicom
echo "\n" >> /dev/ttyACM0
sleep 5
echo "\n" >> /dev/ttyACM0
echo "run x_boot" >> /dev/ttyACM0
sleep 5
echo "root" >> /dev/ttyACM0
sleep 3
echo "cd /tmp" >> /dev/ttyACM0
sleep 1
In the above code all the echo commands i'm passing directly to the device file not to the minicom terminal.
Requirements:
Now I need to send command1 to minicom
Make the terminal to sleep for 5 sec before sending next command
Send Command2
Wait again for 5sec.
And many commands automated
After that exit the terminal without closing minicom
Please help me with this.
Use minicom scripting (runscript) instead of bash echoes. It has both send and sleep commands:
-S, --script=SCRIPT : run SCRIPT at startup

Linux screen push command error

#!/bin/sh
SERVER=$1
RCON=$2
echo "$SERVER"
echo "$RCON"
sudo -u flash screen -r $SERVER -X stuff "$RCON"`echo -ne '\015'`
Guys do you see something wrong with this code? The main concept in the script is to push a command to a opened screen session.
In particular, it's expected to execute a command into opened screen session.
The script echoes the two vars, but when I open the screen, the command $RCON isn't executed.
replaced
#!/bin/sh
with
#!/bin/bash

Resources