Run a Bash Script automatically upon login on Unix - bash

I have a bash script that I've written that works if I execute from the terminal, but I want to be able to have this script ran when I login to my system. I have seen other questions similar to this, but the proposed solutions did not work for me. I tried adding the script path to my ~/.profile and the script still is not being ran. Anyone have an example or documentation on how to do this?
Side note I am using Unix

If you already had a ~/.profile file it should be fine to just add it there. Otherwise look for a ~/.bash_profile and add the line there.
Did you make sure that your file is executable? Otherwise it will not work.
Here is an example code (make sure to adapt to your needs):
echo "echo 'foo'" > /tmp/execute_me
chmod u+x /tmp/execute_me
echo "/tmp/execute_me" >> ~/.profile
login from another console (for safety), and you should see "foo" printed in your console somewhere.
If you want your script to be executed whenever a shell is used (even not interactive, you should add the code to the ~/.bashrc, read this for details: https://unix.stackexchange.com/questions/129143/what-is-the-purpose-of-bashrc-and-how-does-it-work)

The script should be called .profile and in your scripts directory.

I don't if your system supports it, but calling scripts (as root or as a common user , via su - user -c "command") from /etc/rc.local works great on my "nix" system. You might want to add some delay and possibly pass the display variable as well. Example of rc.local entry:
sleep 20 && bash -c "env DISPLAY=:0.0 nohup /folder/script &"

Related

Raspbian: Reset Bash environment variables

I was trying to get a crontab working on my Raspberry PI and I think I messed up my environment variables. I can execute a file from the GUI by right-clicking and choosing execute. However I cannot get the same file to run from command line. I can use ls to see the file (ChromeTab.sh), but when I type ChromeTab.sh, I get "bash: ChromeTab.sh: command not found".
I think I messed up my environment variables when I put this in the crontab.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
I followed the examples in Script doesn't run via crontab but works fine standalone.
Any idea what I'm doing wrong?
UPDATE:
OK,
Let me clarify what efforts I took on my part BEFORE posting my question on stackoverflow before getting anymore downvotes.
First of all thanks S. Adam Nissley for your suggestions.
In answer to your steps listed above.
Running this from home path, or fully qualified path does Not work as stated.
Error: bash: ChromeTab.sh: command not found
./ChromeTab.sh
I have also ensured read/write and execute permissions on the file with
chmod +x ./ChromeTab.sh
Also, my bash script starts off with the following shebang
#!/bin/sh
So, what i'm trying to say is, regardless of using crontab or not the issue at hand is that I can not even execute the script from command line. This started happening after I was messing around with the environment variables in the crontab. I'm looking for a way to revert to the situation where I can at least run/execute bash commands from the terminal.
The only way I can effectively execute this script is (right-click execute) through the GUI.
Assuming you are in the same directory as your script, you should just be able to enter
./ChromeTab.sh
If it does not execute, make sure it is executable with the command
chmod +x ./ChromeTab.sh
Or
chmod 755 ./ChromeTab.sh
And if it still won't execute, make sure it has an appropriate hashbang on the very first line of the script like #!/bin/sh or #!/bin/bash
When you add it to your crontab, make sure it has the full path like
/home/pi/bin/ChromeTab.sh <br/>
EDIT: Default PATH and SHELL for Raspbian
You can check your PATH and SHELL environmental variables from the command line as follows:
echo $SHELL
echo $PATH
The default PATH for Rasbian is:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
And the default SHELL is:
/bin/bash
So if you need to set those it is as simple as:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
SHELL=/bin/bash
If you are having other issues with your environment, you may want to disable some of your local settings to see if the problem is in your profile. You can list all files with ls -a, which includes hidden files. Raspbian typically has a .bashrc and a .profile in each user's home directory. To disable them simple rename them:
mv .bashrc .bashrc_disabled
mv .profile .profile_disabled
If that solves the problem, you can inspect the files and make the necessary corrections before renaming them back to their original names.

How to run a shell script just by typing its name?

I am very stuck on a task for my controlled assessment. I have been asked to create a shell script in nano that echoes hello world and hello $user. I have set the script to be executable. (chmod 755 Script1 and chmod +x Script1). I have been tasked to be able to make the script run just by typing 'Script1' , I do not know how to set the path to do this. Any help would be greatly appreciated.
Basically you need to have it in the PATH environment variable.
Suppose it's in a folder on /a/b/folder/script.sh, try:
PATH=$PATH:/a/b/folder/
This tells the shell to look for executables there when looking for commands to run.
To make sure it's always executable you have several options:
Set the path variable in your profile using ~/.bashrc (sets it for bash, commonly done in ubuntu, which is what I use) or ~/.profile.
Set it globally using /etc/environment or others...
Move the folder to a folder already on your path (echo $PATH to see what's there already).
Note that these files are source-ed on different stages and persist on different environments.
For even more information read this.
TIP: To test that you've set the file correctly, we'll take ~/.bashrc as an example, you can use source ~/.bashrc and everything in ~/.bashrc will be loaded. That way you can be sure it won't mess things up next time you log in.
Alias the script in .bash_profile
in home path open the .bash_profile file and add the following entry,
alias Script1='/path/to/Script1'
Then reload the bash by,
. .bash_profile
After that try to call the script by using just its name. i.e, Script1

simply bash script doesn't work

#!/bin/bash
echo "hello"
I run this script and I have got only empty space why? I am sure my code is okay, so why don't show text's?
please specify what the problem is.
If your script doesn't run:
For running scripts you should make it executable:
chmod +x script.sh
Another thing that you should know is about SELINUX you should make sure it is sat or not...and specify at which directory it is running?
please run this command and let me know:
ls -l scipt.sh
IF echo command doesn't work:
At first run it in your terminal and see the result...may be the command echo is removed or not...
try to run this bash:
#!/bin/bash
echo "hello"
echo $?
and try this one:
#!/bin/bash
STR="Hello World!"
echo $STR
And let me know about output.
better to ask your question at unix Q&A or superuser
Execute the below commands in the terminal to troubleshoot.
Check to see what shell you are on.
The below command would tell you your current shell.
echo $SHELL
Once you confirm that make sure that you have the bash shell present
in the /bin directory.
/bin/bash
echo "hello"
make sure u've already made your content to your script file. In some case, if u use some editor like vscode it has some option that won't save content to the file.If this is your case, u may change the option.
I ran into the same puzzling issue when running my hello world script from a terminal window, using Ubuntu. I was editing the file using gedit, giving all permissions from the properties gedit functionality, and when running ./hello_world.sh, further chmoded to +x from a terminal window as well, it simply wouldn't show the result of my echo "Hello World" command.
Needless to say as I have been through all the above answers and all showed my script should have worked.
I simply solved it by editing the script using vim from a terminal window! Then chmod +x again just in case, ran the script and Hello World finally appeared!
But I don't know why it didn't work using the gedit edition;

Shell script to use my login environment

I have crontab running a shell script periodically. I need the script to run in the same environment that I usually log in. Can I just simply add this line in 2nd line of the script (after shebang).
source /home/<my username>/.cshrc
Or what's the proper way to set the cron shell process to use my login environment?
PS: I am quite sure which exactly setting is needed by my script, so I can only source the whole .cshrc.
Try something like that:
sudo su - <user> -c <cmd>
Of course you have to alter the sudoers file first.
Take a look at the man page.
hth

Activating a VirtualEnv using a shell script doesn't seem to work

I tried activating a VirtualEnv through a shell script like the one below but it doesn't seem to work,
#!/bin/sh
source ~/.virtualenvs/pinax-env/bin/activate
I get the following error
$ sh virtualenv_activate.sh
virtualenv_activate.sh: 2: source: not found
but if I enter the same command on terminal it seems to work
$ source ~/.virtualenvs/pinax-env/bin/activate
(pinax-env)gautam#Aspirebuntu:$
So I changed the shell script to
#!/bin/bash
source ~/.virtualenvs/pinax-env/bin/activate
as suggested and used
$ bash virtualenv_activate.sh
gautam#Aspirebuntu:$
to run the script .
That doesn't throw an error but neither does that activate the virtual env
So any suggestion on how to solve this problem ?
PS : I am using Ubuntu 11.04
TLDR
Must run the .sh script with source instead of the script solely
source your-script.sh
and not
your-script.sh
Details
sh is not the same as bash (although some systems simply link sh to bash, so running sh actually runs bash). You can think of sh as a watered down version of bash. One thing that bash has that sh does not is the "source" command. This is why you're getting that error... source runs fine in your bash shell. But when you start your script using sh, you run the script in an shell in a subprocess. Since that script is running in sh, "source" is not found.
The solution is to run the script in bash instead. Change the first line to...
#!/bin/bash
Then run with...
./virtualenv_activate.sh
...or...
/bin/bash virtualenv_activate.sh
Edit:
If you want the activation of the virtualenv to change the shell that you call the script from, you need to use the "source" or "dot operator". This ensures that the script is run in the current shell (and therefore changes the current environment)...
source virtualenv_activate.sh
...or...
. virtualenv_activate.sh
As a side note, this is why virtualenv always says you need to use "source" to run it's activate script.
source is an builtin shell command in bash, and is not available in sh. If i remember correctly then virtual env does a lot of path and environment variables manipulation. Even running it as bash virtualenv_blah.sh wont work since this will simply create the environment inside the sub-shell.
Try . virtualenv_activate.sh or source virtualenv_activate.sh this basically gets the script to run in your current environment and all the environment variables modified by virtualenv's activate will be available.
HTH.
Edit: Here is a link that might help - http://ss64.com/bash/period.html
On Mac OS X your proposals seems not working.
I have done it this way. I'am not very happy with solution, but share it anyway here and hope, that maybe somebody will suggest the better one:
In activate.sh I have
echo 'source /Users/andi/.virtualenvs/data_science/bin/activate'
I give execution permissions by: chmod +x activate.sh
And I execute this way:
`./activate.sh`
Notice that there are paranthesis in form of ASCII code 96 = ` ( Grave accent )
For me best way work as below.
Create start-my-py-software.sh and pest below code
#!/bin/bash
source "/home/snippetbucket.com/source/AIML-Server-CloudPlatform/bin/activate"
python --version
python /home/snippetbucket.com/source/AIML-Server-CloudPlatform/main.py
Give file permission to run like below.
chmod +x start-my-py-software.sh
Now run like below
.start-my-py-software.sh
and that's it, start my python based server or any other code.
ubuntu #18.0
In my case, Ubuntu 16.04, the methods above didn't worked well or it needs much works.
I just made a link of 'activate' script file and copy it to home folder(or $PATH accessible folder) and renamed it simple one like 'actai'.
Then in a terminal, just call 'source actai'. It worked!

Resources