Showing results for 
Search instead for 
Do you mean 

su from cron

Frequent Advisor

su from cron

Hi
I'm trying this from root cron
50 * * * * "su - wmsusr -c /wms/app/zabbix/check_zabbix.sh"

Works fine if I
# su - wmsusr
]: /wms/app/zabbix/check_zabbix.sh
1
2
3
4
5
wmsusr@prodomni ksh[392]:

Here is the script
HOSTNAME=`whoami`
echo 1
value=`ps -fu $HOSTNAME |grep zabbix| grep -v grep| wc -l`
echo 2
if [ $value -eq 0 ]; then
echo 3
rm -f /wms/app/zabbix/agentd.pid
echo 4
/wms/app/zabbix/zabbix_agentd -c /wms/app/zabbix/zabbix_agentd.conf
echo 5
fi
exit 0;
13 REPLIES
Acclaimed Contributor [Founder]

Re: su from cron

Hi:

What does the '/var/adm/cron/log' show; particularly the return code (rc=)?

Regards!

...JRF...
Frequent Advisor

Re: su from cron

I should mention if I run
# su - wmsusr -c /wms/app/zabbix/check_zabbix.sh
from root prompt I get this

1
2
logout
#
Respected Contributor [Founder]

Re: su from cron

you are trying to check if zabbix agent is running as wmsusr, if not remove the pid and start zabbix agent.

your second output ran as root, did you stop the agent before running the script? If the agent is running, you will see only 1 and 2 right?
Acclaimed Contributor [Founder]

Re: su from cron

Hi:

I believe Wilfred may have found the reason.

You really ought to change the *name* of the variable that holds the *user* id. Calling the variable "HOSTNAME" is rather misleading :-)

Regards!

...JRF...
Acclaimed Contributor [Founder]

Re: su from cron

>value=`ps -fu $HOSTNAME | grep zabbix| grep -v grep | wc -l`

You may want to list what $HOSTNAME and "ps -fu $HOSTNAME" shows so you can make sure what you are looking for.
Frequent Advisor

Re: su from cron

Hi All...

Hostname is a bad var name but I don't see how it causes the problem... The agent is not running when I'm running the script and from root

su - wmsusr -c /wms/app/zabbix/check_zabbix.sh

Results
~/.kshrc

1
2
logout

Any ideas? THANKS!
Frequent Advisor

Re: su from cron

Just to confirm again... If I su to wmsusr and then run all is fine... Just when I'm calling from roots cron and trying to su to that user do I have an issue.
Acclaimed Contributor [Founder]

Re: su from cron

HI (again):

> If I su to wmsusr and then run all is fine... Just when I'm calling from roots cron and trying to su to that user do I have an issue.

Why do you 'cron' this as 'root' if you then switch to the 'wmsusr'?

What happens if you 'cron' this task as the 'wmsuer' ?

Regards!

...JRF...
Frequent Advisor

Re: su from cron

Right now only root user has cron and because of service guard they just want one crontab to maintain... Small IT group.
Acclaimed Contributor [Founder]

Re: su from cron

Hi (again):

> Right now only root user has cron and because of service guard they just want one crontab to maintain... Small IT group.

Regardless, would you humor us and try running as a crontask of 'wmsusr' (without the need to 'su')?

> Hostname is a bad var name but I don't see how it causes the problem

Of course not. I merely was pointing out that when you ask someone to look at your code, it always helps to have used *meaningful* variable names. You wouldn't show me an orange and tell me that the apple you have is rotten, would you?

Regards!

...JRF...
Respected Contributor [Founder]

Re: su from cron

what is the result, if you do not invoking a login shell? i.e.

50 * * * * "su wmsusr -c /wms/app/zabbix/check_zabbix.sh"
Frequent Advisor

Re: su from cron

Hi All... I just moved the su into the script and it is fine now...
echo 4
su - wmsusr -c "/wms/app/zabbix/zabbix_agentd -c /wms/app/zabbix/zabbix_agentd.conf"
echo 5

Highlighted
Frequent Advisor

Re: su from cron

See above, opps.