Run bash script after logon is made - bash

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?

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

Is there a way to get my laptop to beep from within a bash script running on a remote server via SSH?

I have a bash script that I have to regularly run on a remote server. Part of the script includes running a backup which takes a while, and after it has run, I have to hit "Y" to confirm that the backup worked before the script will continue.
I would like to know if there is a way to get my laptop to make a beep (or some sort of sound) when that happens. I know that echo -e '\a' makes a beep, but if I run it from within a script on the remote server, the beep happens on the remote server.
I have control of the script that is being run, so I could easily change it to do something special.
You could send the command through ssh back to your computer like:
ssh user#host "echo -e '\a'"
Just make sure you have ssh key authentication from your server to your computer so the command can run smoothly
In my case the offered solutions with echo didn't work. I'm using a macbook and connect to an ubuntu system. I keep the terminal open and I'd like to be informed when a long running bash script is ready.
What I did notice is that if I shutdown the remote system then it will beep the macbook and show an alarm icon on the relevant tab. So I have now implemented a bit of dirty workaround:
sudo shutdown 1440 && shutdown -c
This will initiate the system to shutdown and will immediately cancel the request. And I do get the alarm beep + icon. You will need to setup sudo to allow the user to permit shutdown. As it was my own remote server it was no problem but could limit the usability for others.

How to run screen using bash script

I am developing an application on Linux CentOS server. I need to automatically after registration of each use server create a screen to run some codes in loop for user.
When I use bash script to create new screen, it can't open screen and run commands in that screen.
For example, I want to open an screen and I run a php file in that screen. I have created a bash script test.sh but when I run this using cron tab it doesn't work.
screen
php php.php
Please tell what can I do to solve this?
It's quite easy to do this and such a thread already exists.
screen -d -m yourcommand

Running a script after user logs in

How could a script wait for the login process to complete before running a command in shell script in Mac OS X?
I have tried wait and sleep commands, but that doesn't seem to stop the script running under the root that owns the login process.
I want the script run after the user logs in.
There are 2 easy(ier) options:
Create a Login Item. To see an example, click here.
Use launchd. You don't have to install anything. All you need to do is to create a configuration file to tell launchd what to do, and save it (with proper permissions) on a specific directory that's read by launchd.

Unix - Suppress informatory messages

I am trying to execute a remote command for one of my scripts. I have to run this script across many servers. so i will put it in a script. What I am trying to do is
ssh root#10.158.42.12 nohup perl /script/myscript.pl 06/04/2014 60 &
The script runs just fine but there is an info message which is displayed whenever you try to login . The one many of you would be familiar with ..
|-----------------------------------------------------------------|
| This system is for the use of authorized users only. |
| Individuals using this computer system without authority, or in |
Due to this the script execution is haulted unless an enter is pressed. I want to put the script in a cronjob for automatic execution, so i dont need to see this info message.
Here's my theory:
Your command doesn't run the program in the background on the server. It runs runs the program in the foreground on the server, and then you background ssh.
Since ssh runs in the background, you are immediately returned to your prompt.
Milliseconds later, ssh overwrites your prompt with this message and runs the command.
You are now looking at the ssh message and no prompt.
You hit press enter, which causes the prompt to be redrawn on the next line.
This leads you to believe ssh was actually waiting for you to press enter. In reality, the command was already run, and bash was ready for new commands, just obscured by ssh noise.
How to test:
If I'm right, pressing Ctrl+L instead of Enter will clear the screen and show the bash prompt. (assuming you don't use bash's vi mode).
If I'm not, Ctrl+L will instead either do nothing, print the ssh message again or just write ^L to the screen.
How to fix if I'm right:
ssh -f root#10.158.42.12 'nohup perl /script/myscript.pl 06/04/2014 60 &' 2> /dev/null

Resources