Loop function on Bash freezes my Raspberry Pi while executing xdotool commands - linux

I have a rpi4 8gb that on the startup opens chromium and executes this Bash script
The idea is to refresh the browser every x random interval and execute some key strokes
#!/bin/bash
export XAUTHORITY=/home/sean/.Xauthority
export DISPLAY=:0
while true
do
random=$(( $RANDOM % 1740 + 3540 )) #random number between 29 to 59 minutes
sleep $random
xdotool key F11
xdotool key "Ctrl+Shift+r" &
done
Sometimes I found that the system freezes, what could cause the problem?

My guess is that the & sign on the last line is what causes the freeze. However consider this alternative:
I recommend that instead of making an infinite loop (which could potentially freeze up your system in many ways), just assign it as a cronjob in 30min intervals.
this simplified script without loop should do it:
#!/bin/bash
export XAUTHORITY=/home/sean/.Xauthority
export DISPLAY=:0
xdotool key F11
xdotool key "Ctrl+Shift+r"
crontab -e (choose an editor example nano)
add this line at the end of the file: (runs script every hour:00 and hour:30)
*/30 * * * * /home/sean/autorefresh_chromium.sh add the correct path
ctrl+S to save
ctrl+X to exit
That's it. It will run in the background every 30min, even after reboots. The benefit is that this way the script refreshes the browser and exits, so it is not constantly sitting on your system and it cannot get stuck.
I also use xdotool, but not on a pi. I recommend to be cautious if you are running in headless mode, because without a monitor and other GUI components xdotool might not behave as expected. Considering that you refresh a webpage I guess you have GUI so it should be fine.

Related

bash script led keyboard in crontab

I use this simple bash script in order to power on my keyboard led in my linux VM .
If i don't use the script i can't power on led.
#!/bin/bash
sleep 1
xset led 3
xmodmap -e 'add mod3 = Scroll_Lock'
When i tried to launch it works , but if i insert in crontab doesn't.
#reboot /home/giacomo/Desktop/keyboard.sh
can you help me ?
i even tried to put in crontab the commands , doesn't work.
I tried insert it in root crontab, doesn't work.
#reboot xset led 3 ; xmodmap -e 'add mod3 = Scroll_Lock'
i want to automatically power on led of keyboard at reboot, without launch it myself.
thanks
Confused on your choice of approach for what you are trying to do.
Why are you not putting that code snippet in your ~/.bashrc-local, which will be left untouched by OS updates that modify ~/.bashrc ?

Making cronjob from AppleScript

I am trying make a crontab run an apple script.
My problem is the script doesn't seem to run. i dont get any error messages or anything the script just doesn't run.
The my apple script just opens the mac calculator. As follows:
tell application "System Events"
get name of every process whose name is "Calculator"
if result is not {} then
tell application "Calculator"
quit
end tell
else
tell application "Calculator"
activate
end tell
end if
end tell
This script runs fine and opens the calculator no problem when I click run button in the script editor window.
I saved this file as calculator_script on my desktop, it is a .scpt file.
To run the cronjob i open terminal and type:
crontab -e
i
23 16 * * * cd Desktop && calculator_script.scpt
press esc
:wq
it says a new cronjob is created but but when the time comes that i specified in the cronjob expression nothing happens.
I can't see where I've gone wrong.
I know the script works because it opens the calculator, so it must be something to do with the cronjob. I make sure that I put the time a couple of minutes into the future in the cronjob expression.
When type crontab -l i see my cronjob in the list. it just never runs for some reason.
iactually i solved it, i needed to use osascript to run the script. so it ended up being:
59 16 * * * cd desktop && osascript calculator_script.scpt

How to use xdotool to open a new tab, switch to it and run commands in it

I am trying to write a bash script to automate running some commands. However some of these commands should be running in their own terminal tab.
So I use the following in my bash script to open a new tab:
xdotool key ctrl+shift+t
this does the job, but the next commands in my bash script are still executed in the previous terminal tab.
How can I make the new opened terminal tab active and run the next commands in this tab?
What Terminal Emulator are you using? It strongly depends on this.
In general, you could write the commands you want to execute in a shell script and tell your terminal emulator to execute the script once it has started.
Example with xterm:
echo '#!/bin/bash' > /tmp/thescript
echo 'ls -la' >> /tmp/thescript
chmod +x /tmp/thescript
xterm -hold -e /tmp/thescript
EDIT: I just saw that u asked for a way to achieve this with xdotool. So this answer might be invalid. Please tell me if so - then i'll delete it.
How are you using xdotool? It can be done with a chain, for example:
$ xdotool key "ctrl+shift+t"; xdotool type "ls"; xdotool key Return
If all you want is to run the commands in the background / in parallel, without synchronously waiting for each command to complete before the next begins, terminate them with an ampersand & to instruct the shell to do so.
Alternatively, you can execute the commands in their own subshells by surrounding each with parentheses ( ). If they are long running processes or you do not wish to pollute the original shell with their output, you can fork them off and capture their output to file with something like (setsid command 1>/path/to/log &).
If separate tabs is necessary requirement, you can use xdotool to key the switch-to-the-next-tab binding or similar, and then key the commands you must run in that tab.
Instead of sorting out that mess yourself, you could use a script from this answer by Jacob Vlijm, which wraps a windowed approach that uses xdotool and wmctrl to 'send' commands to different terminal windows. The script is written in python 3 but it can easily be rewritten for a shell environment of choice.
A more direct approach involves use of a TIOCSTI ioctl to inject characters into another terminal. According to the tty_ioctl manual page:
NAME
ioctl_tty - ioctls for terminals and serial lines
...
DESCRIPTION
The ioctl(2) call for terminals and serial ports accepts many possible
command arguments.
...
Faking input
TIOCSTI const char *argp
Insert the given byte in the input queue
...
Here are c and perl wrappers, and an example in python as referenced by this answer.

Keep SSH Sessions running after disconnection

I am using my laptop via shell terminal to log on school’s server to run a
Matlab session. The session will take about 10 hours and I want to close my
laptop, go home, have dinner, and re-engage the shell terminal to check the
progress of my Matlab session.
From this link I know I should use nohup nohup to keep my terminal alive,
but I meet the following problem. Here is a screenshot of my shell after I start
running Matlab session:
where a = cv000_29590 is the respond from the Matlab. It should keep running
until cv999999 and take about 10 hours.
The problem is, this shell is not interactive anymore. I can’t enter anymore
commands, that is, I have no where to enter nohup commend to keep my SSH
session alive.
It's not really possible after you've already started a session. But for new sessions you can do the following:
Add the following to the top of your .bash_profile:
if [ -z "${PS1}" ] ; then
return
fi
if [ "${TERM}" != "screen" ] ; then
export HOSTNAME
exec screen -xRR
fi
function new {
u=${1:-$USER}
test ${u} = ${USER} && screen -t ${u}#${HOSTNAME} || screen -t ${u}#${HOSTNAME} su --login ${u}
}
Put the following content into .screenrc:
escape ^bb
shell -$SHELL
termcapinfo xterm ti#:te#
hardstatus lastline "%-Lw[%n%f %t]%+Lw%<"
screen -t ${USER}#${HOSTNAME}
These are mostly my own customizations of screen. The most important of which is that I set the screen escape character to CTRL-b instead of the default CTRL-a so I can still use CTRL-a in bash to go to the beginning of a line.
Use CTRL-b c to create shells in new windows (or just type new at the bash prompt to use the function). And use CTRL-b d to detach your session and leave it running. Next time you login, you'll be reattached to your session and everything will be as it was. Use CTRL-b n to cycle through the windows you've created. If you don't want to multiple windows, you don't have to, just use the ability to leave a session running and reattach later.

wmctrl not working in crontab

I'm using Linux (Ubuntu). I use wmctrl to make the firefox window always on top. And it worked FINE when I run the shell on a terminal.
Here is my shell code (say that it was /usr/app/keepfront.sh):
#!/bin/bash
WINTITLE="Mozilla Firefox" # Main Firefox window has this in titlebar
PROGNAME="firefox mywebsite --sync" #run the firefox program
#Use wmctrl to list all windows, count how many contain WINTITLE
WINCOUNT=wmctrl -l | grep -c "$WINTITLE"
if [ $WINCOUNT != 0 ]
then
wmctrl -a "$WINTITLE" # If it exists, bring window to front
else
$PROGNAME & # Otherwise, just launch ff
fi
exit 0
I would like to use crontab to run the shell every 1 minute. Crontab DID run the shell (I wrote some echos), but nothing happened.
Here is my crontab code:
*/1 * * * * /usr/app/keepfront.sh
Anyone know WHY? How to solve this?
cron jobs do not have access to your environment variables, although they are owned by the user they do not run in that user's full desktop environment. In this case your script does not know about your DISPLAY environment variable. To retrieve information and to make changes wmctrl needs to know which DISPLAY to use.
To do what you want to do, all you need is to set the DISPLAY environment variable in your script before any calls to wmctrl. Assuming you only have 1 monitor the line below should fix your problem (my test worked fine). If you have more than 1 monitor then just use echo $DISPLAY on the command line to help you configure the command for your various monitors.
# Add to your script before any calls to wmctrl.
export DISPLAY=:0
Some other things to note:
If you have more than one 'Mozilla Firefox' window open then your code will only bring the first one encountered by wmctrl to the top, this will be the one that was opened first because wmctrl looks through windows from oldest to newest.
I have not tested the launching of Firefox aspect of your script, genarally speaking I would have thought doing this is a bad idea because Firefox will also use environment variables which won't be set when running the script from crontab. You could find a list of all the environment variables that Firefox uses and then manually set them in the script...
You do not need the */1 bit in your crontab line, just use: * * * * * /usr/app/keepfront.sh to run something every minute.
You may wish to add some environment variables to the top of your crontab file - many people do for a variety of reasons. For instance the PATH in your crontab file probably won't be the same as your user PATH and your LANG variable won't be set either, this can stop regular expressions used in scripts called by cron from working. I have the following set at the top of my crontab file, like this:
# These are the basic paths, mine also includes my own scripts path.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Note: LANG allows grep regexes to work properly in called scripts.
LANG=en_GB.UTF-8
Hint: type echo $PATH and echo $LANG to get your current settings.
Typing env on your command line will show you all your environment variables, to see how limited those available to cron are, add this line to your crontab, don't forget to change the path I've used and to remove the line after it has run.
* * * * * env > /home/user/EnvOutputFromCrontab
Hope this helps.
One part of your problem is that this line doesn't do what you think it does:
WINCOUNT=wmctrl -l | grep -c "$WINTITLE"
It runs the command -l (which probably doesn't exist) with WINCOUNT=wmctrl as one of its environment variables.
You probably intended to write:
WINCOUNT=$(wmctrl -l | grep -c "$WINTITLE")
The other part of your problem may be that wmctrl and firefox don't work correctly when run without a terminal, as crontab runs its jobs without a terminal. I've not tried running firefox from crontab, and I can't think of anything much more annoying than having Firefox jump to the foreground every minute (OK; I can think of some things about equally annoying, but the concept doesn't bear thinking about).

Resources