# Running a user crontab



## rwaqa (Nov 28, 2011)

Hi,

I tried to 'cron' a small script using username 'ookla' as indicated below but is facing problem running. This should enable a 'pingserver' to run on tcp/udp port 8080

`# crontab -u ookla -l`

```
* * * * * ~/pingtest.sh 2>&1
```

However manual starting the 'daemon' confirms it works.


```
$ java -jar Server.jar --tcpPort 8080 --udpPort 8080
LOG     Mon, 28 Nov 2011 12:52:13 +1200 MAIN    No IP Specified, will use: xxx.xx.25.245
LOG     Mon, 28 Nov 2011 12:52:13 +1200 MAIN    Start UDP Thread
LOG     Mon, 28 Nov 2011 12:52:13 +1200 MAIN    Start TCP Listener
LOG     Mon, 28 Nov 2011 12:52:13 +1200 S-TCP   Listening on port: 8080
```
I wonder if I am missing something on my 'cron' job?
Please assist...


Best regards,
Waqa


----------



## wblock@ (Nov 28, 2011)

Please show the actual contents of pingtest.sh.  Also, ~/ is a relative path.  Try an absolute path.


----------



## rwaqa (Nov 28, 2011)

Hi wblock@,



```
#!/bin/sh

if [ `ps x | grep VoipServer | grep -v grep | grep -vi screen | wc -l` = "0" ]
then
  cd ~/
  java -Xmx256m -jar VoipServer.jar --tcpPort 8080 --udpPort 8080 > /dev/null &
fi
~
~
```
As requested, I will try the absolute path and verify as well. By the way this is on FreeBSD 8.1.



Regards,
Rwaqa


----------



## wblock@ (Nov 28, 2011)

Rather than all the contortions to see if it's already running, use pgrep(1).  Also, use full paths.


----------



## mix_room (Nov 28, 2011)

If you are starting a server that is going to be running all the time, why are you running it every minute?

I'm guessing the if-statement in your shell script is to check if the program is running. It might be better to have a PID-file, where you can enter the PID. When starting the program you can check if the file exists, and if it exists you can proceed to do nothing, or restart/stop it should this be necessary.


----------



## rwaqa (Nov 28, 2011)

Ok, got a message for /var/mail/ookla as attached below and looks the issue is about permission. How can user 'ookla' be allowed to run cron on Freebsd 8.1?


```
Date: Tue, 29 Nov 2011 04:13:00 +1300 
Message-Id: <201111281513.pASFD0FQ011635@host1.connect.com>
From: [email]ookla@host1.connect.com[/email] (Cron Daemon)
To: [email]ookla@host1.connect.com[/email]
Subject: Cron <ookla@host1> /home/ookla/pingtestcheck.sh 2>&1
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/ookla>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=ookla>
X-Cron-Env: <USER=ookla>

/home/ookla/pingtestcheck.sh: Permission denied
```

mix_room,

This is needed to be run every minute as a requirement from our overseas partner who provides this particular service  


Thanks guys
Waqa


----------



## SirDice (Nov 28, 2011)

rwaqa said:
			
		

> ```
> /home/ookla/pingtestcheck.sh: Permission denied
> ```


`# chmod 755 /home/ookla/pingtestcheck.sh`


----------



## rwaqa (Nov 28, 2011)

Thank you SirDice,

Yes, changing the file script to executable made cron work. But I have another issue pop up, /var/mail says 
	
	



```
java not found
```


```
From: [email]ookla@host1.connect.com[/email] (Cron Daemon)
To: [email]ookla@host1.connect.com[/email]
Subject: Cron <ookla@host1> /home/ookla/pingtestcheck.sh 2>&1
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/ookla>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=ookla>
X-Cron-Env: <USER=ookla>

java: not found

/home/ookla
$ ls -l
total 44
-rw-r--r--  1 ookla  wheel  40280 Nov 25 18:05 VoipServer.jar
-rw-r--r--  1 ookla  wheel     34 Nov 29 03:45 pingtest.cron
-rwxr-xr-x  1 ookla  wheel    186 Nov 29 03:46 pingtestcheck.sh
```
However manually checking confirms java works!

```
$ java -jar VoipServer.jar --help

Usage:
  VoipServer [--help] --tcpPort <tcpPort> --udpPort <udpPort> <externalIp>

Server Daemon for the Ookla Voip Test and the Line Quality Test


  [--help]
        Prints this help message.

  --tcpPort <tcpPort>
        The port the server will bind to for TCP features. (default: 5060)

  --udpPort <udpPort>
        The port the server will bind to for UDP features. (default: 5060)

  <externalIp>
        IP used to access the server (default: interface IP) (default: local)
```

Please help.

Thankyou in advance.
Waqa


----------



## SirDice (Nov 28, 2011)

> ```
> X-Cron-Env: <PATH=/usr/bin:/bin>
> ```



Cron has a very limited PATH. Specify the full path to java.


----------



## rwaqa (Nov 28, 2011)

Not so sure how to.

Can I add a line to /home/ookla in .profile specifying 

```
X-Cron-Env: <PATH=/home/ookla/VoipServer.jar>
```



Thankyou,
Waqa


----------



## wblock@ (Nov 28, 2011)

cron does not log in as the user, so it does not have the full user environment.  Again, use full paths in the script:

```
/usr/local/bin/java ...
```

or, more readably:

```
JAVA="/usr/local/bin/java"

$JAVA ...
```

or, more precisely:

```
JAVA="/usr/local/bin/java"

${JAVA} ...
```


----------



## rwaqa (Nov 28, 2011)

wblock@,

So I edit crontab and should read as below;


```
JAVA="/usr/local/bin/java"
${JAVA}
* * * * * /home/ookla/pingtestcheck.sh 2>&1
```

Almost there I guess.


Thanks,
Waqa


----------



## rwaqa (Nov 28, 2011)

Ok, managed to change the full path java on crontab but error message received from /var/mail/ookla as seen below;


```
From: [email]ookla@host1.connect.com[/email] (Cron Daemon)
To: [email]ookla@host1.connect.com[/email]
Subject: Cron <ookla@host1> /home/ookla/pingtestcheck.sh 2>&1
X-Cron-Env: <PATH=/usr/local/bin/java>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/ookla>
X-Cron-Env: <LOGNAME=ookla>
X-Cron-Env: <USER=ookla>

ps: not found
grep: not found
grep: not found
grep: not found
wc: not found
[: =: unexpected operator
```
Hope this helps in isolating my issue.


Thanks.


----------



## DutchDaemon (Nov 29, 2011)

rwaqa, start formatting your posts!


----------



## rwaqa (Nov 29, 2011)

Noted DutchDaemon,

I guess the error log below relates to the script in my pingtestcheck.sh. Note, *grep*, *wc*, are commands in the script pingtestcheck.sh. Please help as this the last piece in my project to work.


```
Subject: Cron <ookla@host1> /home/ookla/pingtestcheck.sh 2>&1
X-Cron-Env: <PATH=/usr/local/bin/java>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/ookla>
X-Cron-Env: <LOGNAME=ookla>
X-Cron-Env: <USER=ookla>

ps: not found
grep: not found
grep: not found
grep: not found
wc: not found
[: =: unexpected operator
```


----------



## SirDice (Nov 29, 2011)

rwaqa said:
			
		

> So I edit crontab and should read as below;
> 
> 
> ```
> ...


Close.

The JAVA definition should be in your script, not in the crontab.


----------



## rwaqa (Nov 29, 2011)

Thankyou so much SirDice and all who assisted.
My java is running now on its own


```
38889 ookla      10  76    0   406M 17192K sbwait   0:00  0.00% java
```


Big Cheers to all you guys



RWaqa


----------



## wblock@ (Nov 29, 2011)

rwaqa said:
			
		

> wblock@,
> 
> So I edit crontab and should read as below;
> 
> ...



No, put that in the script, along with definitions for all the other external commands.  The only thing in the crontab should be the full path to the script.


----------

