linux shell : how to call programs after a while - linux

when start virtual box if I remap my key, that key will not pass to virtualbox, so I must set the xkbmap to default us, then start vm, then wait a moment call xmodmap ~/.Xmodmap
setxkbmap us
VirtualBox --startvm XP &
#wait a moment
xmodmap ~/.Xmodmap
how to write this shell script?

sleep # where # is number of seconds to wait
setxkbmap us
VirtualBox --startvm XP &
#wait 5 seconds
sleep 5
xmodmap ~/.Xmodmap

Just sleep:
sleep N

A better way to do this would be to wait until the server is not started. You might be able to do below to find this out:
echo `VirtualBox status | grep started | wc -l`

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.

Linux - Launch recordmydestop during specific time

I have some trouble using "recordmydesktop" (capturing video of the computer screen) in command line.
I have a shell script that find the ID of a specific window and launch recordmydesktop with the window ID.
Here is my script :
recordmydesktop --no-sound --delay 3 --windowid $(xwininfo -name "NAME" | sed -n 's/.*Window id: \([0-9a-fx]\+\).*/\1/p')
NAME is the name of the window to record.
The thing is, I want to record during X seconds, and in the man help, there is no options to do that.
The app can be stopped with "Ctrl+C", but I want to make it automatically after X seconds.
Any idea ?
Thanks for reading me :)
#!/bin/bash
recordmydesktop --no-sound --delay 3 --windowid $(xwininfo -name "NAME" | sed -n 's/.*Window id: \([0-9a-fx]\+\).*/\1/p') &
sleep $1; pkill recordmydesktop
or if you need Ctrl+c:
sleep $1; pkill --signal=SIGINT recordmydesktop
value of pause - first scripts parameter

Run command `at ` 5 seconds from now

As part of a slightly complex script, I need to tell a server to run a simulation. Normally, I would achieve this by doing ssh user#server 'simulation/script'. However, doing so would keep the ssh session alive until 'simulation/script' is done, which is undesirable to me.
I recently learned about the at command, and it seems to fit into my problem well.
What I want to do now is to ssh into my server, and at my simulation script to run in 5 seconds (more than enough time for the ssh connection to be closed). Thus, once the ssh connection is closed within 5 seconds, the server will start the simulation without needing the ssh connection to stay alive.
What I'm having trouble with is the time expression that at needs in order to schedule a job "5 seconds from now"
I have tried the following time expressions, all of which give me errors:
now + 5 seconds
now + 5 sec
now + 5 s
now + 5seconds
now + 5sec
now + 5 s
now+5sec
now+5seconds
now+5s
How can I get my at to run my command "5 seconds from now"?
"at" doesn't have sub-minute resolution but you can fake it:
echo "sleep 5 ; COMMAND" | at now
There's no seconds in at :
man at said :
specification of a date must follow the specification of the time of day. You can also give times like now + count time-units,
where the time-units can be minutes, hours, days, or weeks and
you can tell at to run the job today by suffixing the time with today and to run the job tomorrow by suffixing the time with
tomorrow.
So instead of at, you could use a sleep I think.
See man 1 sleep
If you'd like to run ssh user#server 'simulation/script' without waiting, simply do :
ssh user#server 'simulation/script' &
the command will run in the background.
Moreover, as Rawkode said, nohup will help there.
So finally :
nohup ssh user#server 'simulation/script' &
with nohup, you can quit your terminal and have the ssh process alive.
EDIT: if you want to run the ssh command and close the connection :
ssh user#server 'simulation/script &'
at doesn't use seconds, only minutes/hours/days
What you can do is precede your script with nohup, which will ensure the script isn't killed when you disconnect your SSH session.
ssh server 'nohup yourscript.sh &'
NOTE: Having just played with the above, the SSH connection has to be killed manually.
Another alternative would be screen
screen -d -m yourscript.sh
This will launch a detached screen process that you can reattach to at any time later.
NOTE: I've tested this with the following script and command and it worked perfectly.
SSH command
ssh server.com 'screen -d -m ~/myscript.sh'
myscript.sh
#!/bin/sh
sleep 10
echo "hello world" > /tmp/hello
exit;
Just to note: in man at, I saw there is a -t switch, which will accept date times with seconds - but unfortunately the seconds will be truncated:
$ date; date --date="now +10 seconds" +"%m%d%H%M.%S"; echo "logger AAAA" | at -t $(date --date="now +5 seconds" +"%Y%m%d%H%M.%S")
Thu Feb 5 14:45:57 CET 2015
02051446.07
warning: commands will be executed using /bin/sh
job 8 at Thu Feb 5 14:46:00 2015
... and so the job may actually be scheduled in the past (also, used logger to syslog, because it doesn't look like echoing to terminals' stdout can work from here)
I think it is much easier doing:
sleep n && command
where n is number of seconds.
Redirecting stdin/stdout/stderr in addition to backgrounding the script will allow the SSH session to close immediately after executing the backgrounded command:
ssh hostname "/path/to/script </dev/null >/dev/null 2>/dev/null &"
Source: https://serverfault.com/a/36436/138334.
You can do it using sleep command like that:
bash -c 'sleep 5 ; echo "test"' &
I ran into the same issue today, but I was able to resolve it using nohup
nohup bash -c 'sleep 5 && at now -f script-name.sh'

Linux shell script run multiple shell scripts tabs

I would like to make a .sh that runs multiple other ones .sh in new tabs/windows.
something like inside main.sh
"sh1.sh"
wait 5 seconds to load
"sh2.sh"
wait 5 seconds
"sh3.sh"
You could try xterm -e ~/sh1.sh as your command. It'll close as soon as the script has finished though.
If you need to run them in separate windows simultaneously, you need to background each process.
xterm -e sh1.sh &
sleep 5 # why do you want to pause between invocations?
xterm -e sh2.sh &
sleep 5
xterm -e sh3.sh &
This should probably be refactored to use a loop and/or a wrapper function.
for prog in sh1.sh sh2.sh sh3.sh; do
xterm -e $prog &
sleep 5
done

Linux ACPI Configuration

In my Fedora Core Linux (kernel 2.6.16.11), I've been trying to test the auto-shutdown mechanism associated with the CPU temperature. It does not automatically shutdown properly.
If my current processor temperature is 50 C and I set my ACPI configuration to shutdown at 55 C by executing the following commands at the prompt:
echo -n "55:50:45:40:35:30" > /proc/acpi/thermal_zone/THRM/trip_points
echo -n "1" > /proc/acpi/thermal_zone/THRM/polling_frequency
echo -n "0:0" > /proc/acpi/processor/CPU1/limit
Then running any software which brings my temperature way past 55 C, the system doesn't automatically shut down like I need it to.
But as long as the temperature is above 55 C, if I run the following command again:
echo -n "55:50:45:40:35:30" > /proc/acpi/thermal_zone/THRM/trip_points
Then the system shuts down immediately like I want it to! Does anyone know why the system doesn't go down on its own?
Note that I do have '/usr/sbin/acpid' running.
After running those 3 echo's, try cat /proc/acpi/thermal_zone/THRM/trip_points and see what it's set to. My guess is that it gets reset after you set the other two, so I suggest you run the first echo you wrote only after the other two and it might help.

Resources