# Trouble calling 'startx' in Start-Up Script



## PeterStory (Mar 5, 2011)

Hello everyone! This is my first post!

I'm trying to set-up a Thin Client that when booted will automatically log into a server for vnc.
Right now, I have a bash shell script at */etc/rc.d/thinclient*

```
#!/bin/sh

echo "thinclient script starting..."

SERVER_IP="192.168.1.124"
STARTX_PATH="/usr/local/bin/startx"
VNCVIEWER_PATH="/usr/local/bin/vncviewer"
VNCVIEWER_ARGUMENTS="localhost:1 -geometry 1280x1024 -passwd /etc/thinclient/vncpasswd"

echo "Step 1: Attempting ssh connection as 'ThinAccount' to server at $SERVER_IP"
/usr/bin/ssh -f -i /etc/thinclient/id_rsa -L 5901:localhost:5900 -N ThinAccount@$SERVER_IP

echo "Step 2: *Unimplemented* Server will attempt connection to ThinClient"

echo "Step 3: Calling 'startx' with 'vncviewer'"
$STARTX_PATH $VNCVIEWER_PATH $VNCVIEWER_ARGUMENTS
```

I know that all my lines of code work, because the script runs as expected if executed after having logged in.

```
login: root
password:
FreeBSD-ThinClient# cd /etc/rc.d
FreeBSD-ThinClient# ./thinclient
```
Typing all that produces the expected result, filling the screen with wonderful, fullscreen VNC goodness. 
However, I want the result without the typing, at startup.

The only clue I have is the output at startup.
Check it out!

```
/etc/rc: WARNING: Ignoring scratch file /etc/rc.d/thinclient~
thinclient script starting...
Step 1: Attempting ssh connection as 'ThinAccount' to server at 192.168.1.124
Step 2: *Unimplemented* Server will attempt connection to ThinClient
Step 3: Calling 'startx' with 'vncviewer'
xauth: not found
xauth: not found
xauth: not found
xauth: not found
xauth: not found
xinit: not found
xauth: not found
...
login:
```

Seemingly, the problem is that xorg cannot properly start. But why?

Any help from the friendly FreeBSD community would be much appreciated! Could the problem be not having a $PATH variable?

-Peter


----------



## PeterStory (Mar 5, 2011)

*Dang, yo!*

I feel like a fool! But at the same time I feel good, because my script is working. 
It was just the missing $PATH variable. Oh well.

Is there a more elegant solution than just adding:

```
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
```
?

Now I have a *new problem*, that being that *my script doesn't finish*, so sshd, for example, doesn't get started.
Is there a better place to put this script?

Thanks again,
-Peter


----------



## wblock@ (Mar 5, 2011)

TMTOWTDI.  You could run it from /etc/rc.local.  /etc/ttys is another way, and that leaves the rest of the system fairly normal.  Set up an auto-login entry in /etc/gettytab and use that with the ttys entry.

Also: sh != bash, and using either for root's shell can make for some interesting times later.


----------



## kpedersen (Mar 5, 2011)

You could perhaps add '&' to the end of the startx command to move it to background, so the script finishes.

Or you could create another script called 'start_thinclient' which simply runs your thinclient script with an '&' on the end to move the whole script into the background.


----------



## PeterStory (Mar 6, 2011)

Thank you to both of you! 

I learned a lot trying wblock's suggestions, but I eventually decided that, for security reasons, I actually don't want any users logged in. 

Strangely enough, adding '&' to the end of the startx command didn't work. If I remember correctly, it managed to black out the screen. However, creating a 'start_thinclient' script in /etc/rc.d/ and in that running my main script in the background worked great!

Excellent!
-Peter


----------



## wblock@ (Mar 6, 2011)

Not sure exactly what you mean, but avoiding the autologin means you'll be running as root.  An autologin entry doesn't show a login, it just logs in automatically.  In this case, as a non-root user to run X.  Because I've done this several times but forget how each time, I'm going to show it here:

/etc/gettytab

```
# WB: autologin console as user "user"
A|Al|Autologin console:\
        :ht:np:sp#115200:al=user
```

Then use that entry in ttys so user will automatically log in when the system starts that ttyv (at startup):
/etc/ttys

```
ttyv1   "/usr/libexec/getty Al"       xterm  on  secure
```

user's shell is set to /bin/csh in /etc/passwd.

Finally, add code to start X in "user"'s shell startup.  For csh(1), it goes at the end of .cshrc.
/home/usr/.cshrc

```
# WB: startx if it isn't already running
set XPID = `/usr/bin/pgrep xinit`
if ( { [ -n "$XPID" ] } ) then
  #echo "X is already running"
else
  startx
  logout
endif
```

X will use the .xinitrc in user's home directory.


----------



## torqueturns (May 12, 2021)

As I have updated our lab to 12.2-RELEASE, I switched back to xstart for headless clients.  
This thread was the perfect solution.
Thank you wblock@ I have updated your script for us bash shell users:

for .bashrc


```
#  startx if it isn't already running
XPID=`/usr/bin/pgrep Xorg`
if  [[ -z $XPID ]]
then
  /usr/local/bin/startx
  logout
fi
```


----------

