Can you show me how to run a bash file in Terminal immediately after startup?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
DangerousChemistry

Can you show me how to run a bash file in Terminal immediately after startup?

Post by DangerousChemistry »

I'm using Linux Mint 18.3 Cinnamon.

When I double click the .bash file it asks if I want to run or display the file. I click on "Run In Terminal" and the script starts. So it works fine when I manually open it.

I want the script to run automatically. I tried using the Startup Applications in System Settings. I added a custom command but that isn't working. When I click "Run Now" in Startup Applications nothing happens. Nothing happens when I restart either.

Does someone know how to run a .bash file in the Terminal immediately after booting up?

Thank you
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
all41
Level 19
Level 19
Posts: 9498
Joined: Tue Dec 31, 2013 9:12 am
Location: Computer, Car, Cage

Re: Can you show me how to run a bash file in Terminal immediately after startup?

Post by all41 »

Open an instance of Nemo--then in the toolbar Edit/Preferences/Behavior and make sure Nemo will run executable text
files without asking each time.
Welcome aboard the forums.
Everything in life was difficult before it became easy.
User avatar
Termy
Level 12
Level 12
Posts: 4254
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Can you show me how to run a bash file in Terminal immediately after startup?

Post by Termy »

You may need to specify bash FILE, where FILE is the filename of your script. Failing that, amend it to ~/.profile. Does it need root privileges? If so, consider amending it to /etc/rc.local but ensure it is written correctly, regarding exit statuses. Back up any files you edit beforehand, just in-case.

What is the script you're running? What is the command you've used to execute the script?
I'm also Terminalforlife on GitHub.
lmuserx4849

Re: Can you show me how to run a bash file in Terminal immediately after startup?

Post by lmuserx4849 »

DangerousChemistry wrote: Thu Feb 22, 2018 6:53 pm I'm using Linux Mint 18.3 Cinnamon.

When I double click the .bash file it asks if I want to run or display the file. I click on "Run In Terminal" and the script starts. So it works fine when I manually open it.

I want the script to run automatically. I tried using the Startup Applications in System Settings. I added a custom command but that isn't working. When I click "Run Now" in Startup Applications nothing happens. Nothing happens when I restart either.

Does someone know how to run a .bash file in the Terminal immediately after booting up?

Thank you
I don't have cinnamon, but these things are often done the same way. I think you did everything right and the script is probably running (as long as you made it executable/chmod u+x), but you aren't seeing the terminal. One way to confirm this is to direct some output to a file, like echo Test $(date) > $HOME/test.log.

When you used the System Settings, it created a file in $HOME/.config/autostart. The file name will be the name you entered followed by ".desktop". If you cat / view that file, it will have a line that looks like Exec=your_command. If you want to see the terminal, change that line to gnome-terminal --command "path/your_command". Now you probably can also type that same line on the command line in System Settings :-)

In your script, to pause and exit, you can put something like read -p "Hit enter"; exit.

For others:
kde terminal: konsole
mate terminal: mate-terminal

Just a little extra:
You can read more about:
- .bashrc and .profile: bash startup files
- autostart files and directories. You'll hear a lot about .desktop files and freedesktop.org.
Sugarcrisp
Level 3
Level 3
Posts: 170
Joined: Sat Mar 31, 2018 6:58 am

Re: Can you show me how to run a bash file in Terminal immediately after startup?

Post by Sugarcrisp »

I just finished creating my own startup that seems to be working perfectly. Best of all it was very easy. I created my bash script and saved it to my scripts folder.

Code: Select all

#!/bin/sh
expressvpn connect smart
Next, I CD to my scripts directory and ran

Code: Select all

chmod 744 expressvpn connect smart
This gives the file executable rights.
Then I ran the following to add my script to the cron startup utility.

Code: Select all

crontab -e
I use the nano editor (it was one of the options that came up) to create/modify my scripts. I scrolled to the bottom of the file and added
@reboot ( sleep 10 ; sh /home/brett/my-scripts/expressvpnstart.sh )
To save in nano
ctr+o, then enter
To exit in nano
ctr+x
This runs the bash script at reboot/login. The sleep 10 just delays the command for 10 seconds on startup
bash64
Level 3
Level 3
Posts: 116
Joined: Mon Mar 23, 2015 12:19 am

Re: Can you show me how to run a bash file in Terminal immediately after startup?

Post by bash64 »

If you mean after logging into your desktop then Startup Applications is correct.
You need to create an entry that runs an executable bash script.
Lets say its called myscript.sh and it sits in your home folder.

Make it executable:
>sudo chmod u+x myscript.sh

Now, in startup apps ADD a custom command and BROWSE for myscript.sh and fill in the other items.

It will run everytime you login.

If you mean during the boot process then you probably want to add the command to run it to /etc/rc.local:

>sudo bash
>nano /etc/rc.local

/home/{loginname}/myscript.sh

save and exit nano

Replace loginname with your account name.
User avatar
Flemur
Level 20
Level 20
Posts: 10097
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Can you show me how to run a bash file in Terminal immediately after startup?

Post by Flemur »

DangerousChemistry wrote: Thu Feb 22, 2018 6:53 pm I added a custom command but that isn't working. When I click "Run Now" in Startup Applications nothing happens.
You can run a script without a window showing up...so your script might be running but you just don't see anything.

Add something like this to the script, and see if the file "aaa.txt" shows up:

Code: Select all

echo "Script is running " > /home/<username>/aaa.txt
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
User avatar
BG405
Level 8
Level 8
Posts: 2499
Joined: Fri Mar 11, 2016 3:09 pm
Location: England

Re: Can you show me how to run a bash file in Terminal immediately after startup?

Post by BG405 »

Flemur wrote: Tue Apr 10, 2018 7:06 pmAdd something like this to the script, and see if the file "aaa.txt" shows up:

Code: Select all

echo "Script is running " > /home/<username>/aaa.txt
You could also use notify-send "Script running" (or whatever messaage you want) as an alternative.
Dell Inspiron 1525 - LM17.3 CE 64-------------------Lenovo T440 - Manjaro KDE with Mint VMs
Toshiba NB250 - Manjaro KDE------------------------Acer Aspire One D255E - LM21.3 Xfce
Acer Aspire E11 ES1-111M - LM18.2 KDE 64 ----Two ROMS don't make a WRITE
Locked

Return to “Scripts & Bash”