# Run shell script after login



## junkman (Oct 21, 2011)

Hi,

I'm very new to FreeBSD so have mercy. I want to run a shell script after an autologin of root. I've searched the web but i didn't find a solution. I've managed to make the user root to autologin but now I'm stuck at running the shell script after that autologin.

Can somebody explain me what I have to do?


----------



## Abriel (Oct 21, 2011)

Try put script in .login if you are using [cmd=]csh or tcsh[/cmd] SHELL.
For [cmd=]bash[/cmd] SHELL use .bash_profile


----------



## junkman (Oct 21, 2011)

In the .login I have to put only /path/to/the/script.sh? or I have to add some other things?


----------



## Abriel (Oct 21, 2011)

`chmod +x /path/to/the/script.sh`


----------



## junkman (Oct 22, 2011)

Sadly it didn't work. The script is not executed...


----------



## Abriel (Oct 22, 2011)

i tested, I added some fortunes to .login like
`[ -x /usr/games/fortune ] && /usr/games/fortune freebsd-tips`
even i created script, i named it con

```
#!/usr/local/bin/bash
WGET="/usr/local/bin/wget"
$WGET -q --tries=10 --timeout=5 http://www.google.com -O /tmp/index.google &> /dev/null
if [ ! -s /tmp/index.google ];then
        echo "no"
else
        echo "yes"
fi
```
and i putted it it .login like:
`/usr/local/bin/bash con`


----------



## junkman (Oct 23, 2011)

Trying it out.


----------



## wblock@ (Oct 23, 2011)

Abriel said:
			
		

> ```
> #!/usr/local/bin/bash
> ```



Avoid writing bash-specific scripts.  Use #!/bin/sh.  Unless you need bash features, I suppose.



> ```
> WGET="/usr/local/bin/wget"
> ```



FreeBSD has fetch(1) in the base system.


----------



## grigorovl (Oct 23, 2011)

wblock@ said:
			
		

> Avoid writing bash-specific scripts.


Is there any particular reason for this statement? I can't imagine shell scripting if it's not bash.


----------



## _martin (Oct 23, 2011)

grigorovl said:
			
		

> Is there any particular reason for this statement? I can't imagine shell scripting if it's not bash.



Well, you have to expand your imagination then. 

Portability for example. Not every OS (and not even FreeBSD) has bash in its base system. Why would you mess up your script and make it not portable to other systems? What is in your script that you have to use bash-ism?

I totally agree with @wblock on this one. I always swear when I see someone writing plain shell scripts using bash, particularly in the install scripts.


----------



## kpa (Oct 23, 2011)

One argument is that it's still very hard to get bourne shell scripts right, you're dealing with text output from filters with all kinds of different quotation rules and the "programming language" is seriously handicapped when compared to lets say perl or python. Why bother with a bourne shell script when a perl program is much easier to write and get it to do exactly what you want?

On the other hand the times when you have to write the script in bourne shell you really want to keep it minimal and not rely on fancy extensions that only exist in lets say bash.


----------



## wblock@ (Oct 23, 2011)

Beyond the compatibility issues, bash not being part of the base FreeBSD system means that scripts with 
	
	



```
#!/usr/local/bin/bash
```
 will fail when the system boots in single-user mode and /usr/ isn't mounted.


----------



## DutchDaemon (Oct 24, 2011)

That so-called huge difference between bash and sh scripts is very exaggerated. Most bash scripts can be run as sh scripts, simply by changing 
	
	



```
#!/usr/local/bin/bash
```
 to 
	
	



```
#!/bin/sh
```
. The only thing I had to change when I changed some old bash scripts was replacing "==" with "=" in some statements. It's not much work at all.


----------



## SirDice (Oct 24, 2011)

junkman said:
			
		

> I want to run a shell script after an autologin of root.


Man, this almost gave me a heartattack. Autologin of root? What on earth for?

You're not using windows anymore so stop doing everything as root/administrator. Learn to use su(1) and/or sudo(8).

Seriously. This is an accident waiting to happen.


----------



## olav (Oct 24, 2011)

You can also make a rc.d script, if you don't really need to login.


----------



## fluca1978 (Oct 24, 2011)

junkman said:
			
		

> Hi,
> 
> I want to run a shell script after an autologin of root



I hope _autologin_ is a mistake. I will not do autologin even for my grandma!
By the way, why as root? If your problem is a privilige one, than consider using cron, periodic, rc scripts or something alike.
If what you want to do is to _spy_ root (as on a shared machine), than you should consider using sudo and disabling root at all.


----------



## junkman (Oct 24, 2011)

Calm down boys. I use FreeBSD in VirtualBox.


----------



## SirDice (Oct 24, 2011)

junkman said:
			
		

> I use FreeBSD in VirtualBox.


Not relevant in this case.


----------



## fluca1978 (Oct 24, 2011)

Virtualization is not a way to obtain a more secure or a simply secure system.
The scenario you presented is, in my opinion, a bad one. It does not matter you run it with a machine disconnected from the net at all or on a web server.


----------



## _martin (Oct 24, 2011)

> Calm down boys. I use FreeBSD in VirtualBox.



I do understand -- I too have some VMs in VMware with no root password set .. and I sleep ok  [ they are only my personal sandbox though ]

But maybe /etc/rc.local is what you're looking for .. better than some hidden profile woodoo execution. Also note that $profile can be sourced anytime somebody su-es to root, etc.


----------



## junkman (Oct 25, 2011)

matoatlantis said:
			
		

> I do understand -- I too have some VMs in VMware with no root password set .. and I sleep ok  [ they are only my personal sandbox though ]
> 
> But maybe /etc/rc.local is what you're looking for .. better than some hidden profile woodoo execution. Also note that $profile can be sourced anytime somebody su-es to root, etc.



That's my case too, personal "learning sandbox". /etc/rc.locale is not present in my FreeBSD machine.


----------



## SirDice (Oct 25, 2011)

It's /etc/rc.local and the file doesn't exist by default.


----------



## _martin (Oct 25, 2011)

junkman said:
			
		

> That's my case too, personal "learning sandbox".



When you can distinguish between best practice and dirty 'hacks', when you don't bring bad habits to 'real' boxes, you should be ok 

It's worth reading hier(7) and rc(8) man pages.


----------



## W0lf3 (Feb 8, 2017)

I'm using: #! /bin/sh
however, i'm still working on the syntax of how to insert it into the .login 
I want personalized login greetings for each User
My dream is a greeting, the calendar with any schedule events, the last few files opened by the User, and 
OH BOY if I could get the weather stats from Wunderground.com for location of terminal! I be doing backflips for WEEKS


----------



## gordon@ (Feb 9, 2017)

If you are looking for something to run at boot time, you could either create an rc.d script or use `crond` to execute with an @reboot command in the crontab(5).


----------



## ShelLuser (Feb 9, 2017)

W0lf3 said:


> I'm using: #! /bin/sh
> however, i'm still working on the syntax of how to insert it into the .login


That won't work because .login is only used by csh, as is mentioned in the file itself by default. If you're using sh you'd be better off focusing on .profile.


----------

