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 ?
Related
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.
I'm new to bash scripting and need help knowing how to debug this.
This is a TTS script that uses the espek TTS engine to speak the contents of xsel
which holds the contents of whatever the user has highlighted with their cursor.
#!/usr/bin/env bash
if [ $(pgrep espeak) ]; then
killall espeak;
else
espeak "$(xsel)";
fi
The script runs fine when invoked from the terminal ./tts_script.sh. The issue is when I try to invoke it from the keyboard shortcut that I set up in KDE.
I set up the shortcut like this...
go to "System Settings>>Shortcuts>>Custom Shortcuts"
selected "Edit>>New>>Global Shortcut>>Command/URL"
chose a keyboard trigger
then under the action tab I put ~/scripts/tts_script.sh
I tested it by including the following line of code in the top
#!/usr/bin/env bash
espeak "hello there";
I invoked the script with the shortcut and it does speak "hello there" but that's it.
I was able to determine that when the script is invoked from the shortcut it fails as soon as it encounters anything to do with xsel.
Are there any debug logs I can look into?
Are there alternative ways I can invoke the script?
Are there alternative ways I can set a keyboard shortcut in KDE?
Is my code total garbage that makes no sense at all?
Any help is appreciated :)
Using xclip instead of xsel solved the problem
#!/usr/bin/env bash
if [ $(pidof espeak) ]; then
killall espeak;
else
espeak "$(xclip -selection primary -o)";
fi
I created a bash script to use for interactive screen capture and another one for window capture. I'm linking to these with keyboard shortcuts in Linux. The window capture script works without problems:
#!/bin/sh
scrot -u 'ScreenShot_%Y-%m-%d_at_%I:%M:%S-%p.png' -e 'mv $f ~/Pictures/scrot-screenshots'
But the script for for area capture (user selects area with mouse drag) does not work, even though the command works in terminal:
#!/bin/sh
scrot -s 'ScreenShot_%Y-%m-%d_at_%I:%M:%S-%p.png' -e 'mv $f ~/Pictures/scrot-screenshots'
What am I doing wrong? Or maybe a better question is what is preventing the script from letting me select an area of the screen?
I manged to get it working by adding a delay to give the giblib resource time (2/10 of a second) to load:
#!/bin/sh
sleep 0.2 ; scrot -s 'ScreenShot_%Y-%m-%d_at_%I:%M:%S-%p.png' -e 'mv $f ~/Pictures/scrot-screenshots'
How I found the solution:
I couldn't figure out how to get errors to output to a file because running my script from terminal didn't produce any errors. Double clicking the script ran properly and script > file 2>&1 in terminal didn't give me any errors because it ran properly from terminal. I only had errors when I tried to use the keyboard shortcuts (keybindings) attached to the second command from my original post. To see the error that finally lead to the above solution, I downloaded:
`apt-get install xbindkeys` && `apt-get install gconf-editor`
I ran gconf-editor and used the Run Action to executed the script the same manner it would be executed if I was using the keybindings...but attached to a terminal output. That gave me the error output I needed to see:
giblib error: couldn't grab pointer:Resource temporarily unavailable
Which lead me to this post:
https://bbs.archlinux.org/viewtopic.php?id=86507 for the tip.
For whomever the jtlindsey's answer did not work for solving the problem :
giblib error: couldn't grab pointer:Resource temporarily unavailable
Another solution could be this : just before calling scrot, run the command :
xdotool key XF86Ungrab
This should release the pointer, and the scrot command should work after it.
Note : the source claims that before executing previous xdotool command, it would possibly be required to execute :
setxkbmap -option grab:break_actions
I'm lazy, and I prefer that computers do my work for me. I ssh into several machines on a daily basis, so I created a simple script that launches some xterm windows and places them in positions I want (as you can see, I'm using bash):
#!/bin/bash
xterm -geometry 80x27+1930+0 &
xterm -geometry 80x27+2753+0 &
xterm -geometry 80x27+1930+626 &
xterm -geometry 80x27+2753+626 &
However, the next thing I do is go to the first window and type in
ssh server_a
then in the second
ssh server_b
and so on. What I'd like to do is have my script do the ssh commands in each xterm window, and then leave the windows open for me to do my work. I've seen the -e option for xterm, but the window closes after I execute my command. Is there a way to do this?
I apologize if this is a duplicate question. I've searched around and haven't had any luck with this. Many thanks!
I'd love to see a more elegant answer, but what I came up with does work:
xterm -e bash -c 'echo foo; exec bash'
Replace echo foo with the command of your choice, and you're good to go.
This answer gives one of the best answers I've seen so far to do this. Use the bash --init-file flag either in the shebang or when executing the terminal:
#!/bin/bash --init-file
commands to run
... and execute it as:
xterm -e /path/to/script
# or
gnome-terminal -e /path/to/script
# or
the-terminal -e bash --init-file /path/to/script/with/no/shebang
My only real complaint with the exec option is if the command executed prior to exec bash is long running and the user interrupts it (^C), it doesn't run the shell. With the --init-file option the shell continues running.
Another option is cmdtool from the OpenWin project:
/usr/openwin/bin/cmdtool -I 'commands; here'
# or
/usr/openwin/bin/cmdtool -I 'commands; here' /bin/bash
... where cmdtool injects the commands passed with -I to the slave process as though it was typed by the user. This has the effect of leaving the executed commands in the shell history.
Another option is to use gnome terminator. This creates and positions terminals interactively, and you can set up each terminal to run commands within terminator preferences.
Also does lots of extra tricks using keybindings for things like move, rotate, maximise/minimise of terminals within the containing terminator window
See: https://superuser.com/a/610048
"ClusterSSH controls a number of xterm windows via a single graphical console window to allow commands to be interactively run on multiple servers over an ssh connection"
https://github.com/duncs/clusterssh/wiki
$ cssh server_a server_b
$ command
I'm lazy, and I prefer that computers do my work for me. I ssh into several machines on a daily basis, so I created a simple script that launches some xterm windows and places them in positions I want (as you can see, I'm using bash):
#!/bin/bash
xterm -geometry 80x27+1930+0 &
xterm -geometry 80x27+2753+0 &
xterm -geometry 80x27+1930+626 &
xterm -geometry 80x27+2753+626 &
However, the next thing I do is go to the first window and type in
ssh server_a
then in the second
ssh server_b
and so on. What I'd like to do is have my script do the ssh commands in each xterm window, and then leave the windows open for me to do my work. I've seen the -e option for xterm, but the window closes after I execute my command. Is there a way to do this?
I apologize if this is a duplicate question. I've searched around and haven't had any luck with this. Many thanks!
I'd love to see a more elegant answer, but what I came up with does work:
xterm -e bash -c 'echo foo; exec bash'
Replace echo foo with the command of your choice, and you're good to go.
This answer gives one of the best answers I've seen so far to do this. Use the bash --init-file flag either in the shebang or when executing the terminal:
#!/bin/bash --init-file
commands to run
... and execute it as:
xterm -e /path/to/script
# or
gnome-terminal -e /path/to/script
# or
the-terminal -e bash --init-file /path/to/script/with/no/shebang
My only real complaint with the exec option is if the command executed prior to exec bash is long running and the user interrupts it (^C), it doesn't run the shell. With the --init-file option the shell continues running.
Another option is cmdtool from the OpenWin project:
/usr/openwin/bin/cmdtool -I 'commands; here'
# or
/usr/openwin/bin/cmdtool -I 'commands; here' /bin/bash
... where cmdtool injects the commands passed with -I to the slave process as though it was typed by the user. This has the effect of leaving the executed commands in the shell history.
Another option is to use gnome terminator. This creates and positions terminals interactively, and you can set up each terminal to run commands within terminator preferences.
Also does lots of extra tricks using keybindings for things like move, rotate, maximise/minimise of terminals within the containing terminator window
See: https://superuser.com/a/610048
"ClusterSSH controls a number of xterm windows via a single graphical console window to allow commands to be interactively run on multiple servers over an ssh connection"
https://github.com/duncs/clusterssh/wiki
$ cssh server_a server_b
$ command