LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-04-2011, 03:30 PM   #1
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Rep: Reputation: 8
"ps" command not executed in script when using crontab


Hello,

I wrote a simple script to see if firefox is running...
However, the ps command is not executed, when I open the logfile I see: "DEBUG LINE 4" and not DEBUG LINE 5, so "ps -e" is never executed despite using the full path of the ps command.

Here is my script:

#!/bin/bash -l
# simple script to test if firefox is running...

PS=/usr/bin/ps echo "DEBUG $LINENO"
$PS -e | fgrep firefox echo "DEBUG LINE $LINENO"

What am I doing wrong?

Thanks!!

Last edited by Annielover; 06-04-2011 at 03:32 PM.
 
Old 06-04-2011, 04:07 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
It's "/bin/ps", not "/usr/bin/ps".
 
Old 06-05-2011, 04:23 AM   #3
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Original Poster
Rep: Reputation: 8
I changed it to "/bin/ps" but still not working...
I get the following error message:

StartFirefox.sh: line 5: -e: command not found

so the variable $PS is not recognized...

But my script used to be like that:

#!/bin/bash
#Script to start firefox

ps -e | fgrep firefox
if [ $? -eq 1 ]; then
#command to start firefox

It ran fine when executing manually, but with crontab, it does not work. So I thought someting could be wrong with the ps command, but no...
 
Old 06-05-2011, 04:51 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can't print/echo in a cron job unless the output is redirected to /dev/null or a file. There is no
console attached to the program.
 
Old 06-05-2011, 04:54 AM   #5
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Original Poster
Rep: Reputation: 8
This is my full script, it runs fine when I execute it manually, but NOT with crontab... how come? :O

PS=/bin/ps
$PS -e | grep "process name"
if [ $? -eq 1 ]; then
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/laurent/ screen -d -m ./"start binary"& echo "LINE $LINENO"
echo "`date +"%A %d %B %Y, %H:%M:%S"` --> process started."
else
echo "`date +"%A %d %B %Y, %H:%M:%S"` --> process is already running."
fi
 
Old 06-05-2011, 05:46 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Doing this in cron makes no sense. For one thing you are writing to STDOUT which you don't have access to. For another cron runs programs as root unless your system has PAM configured to allow regular users to enter cron jobs.

I'm not certain if you can reattach a screen session if there is not a terminal to attach the screen session to in the first place. You probably have no terminal open with read/write access for screen to be attached to. In the very least, doing so might need to be done before the ps command, or use a variable for the output of the ps command, and redirect stderr to /dev/null. You may need to use ps to indentify processes running in terminals.
E.G.: ttys=$(ps -o tty -u jschiwal | sed '1d;/^\?/d')
Reason being that if you don't have any terminals open, you don't have permission to read or write to the device.

Lastly, why do you want to launch firefox in the background. Why can't you use your eyes to see if firefox is running. It is an interactive program, not a service that runs in the background.
 
Old 06-06-2011, 02:22 PM   #7
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Original Poster
Rep: Reputation: 8
Well okay, to keep it simple, I removed all that "ps" and "screen" crap, and this is my very simple script now:
To start a (LEGAL) game server at reboot:
When this works, I will attach it to screen afterwards...

#/bin/bash
#StartServer.sh
/home/laurent/Games/Cod4/co4_lnxded +exec server.cfg +map_rotate&

It runs perfectly fine when executing manually, but not with cron.
This is my crobtab using: 'sudo crontab -e':

@reboot cd "/home/laurent/Games/Cod4"; ./StartServer.sh

When I reboot my server and ps the process, it doesn't exists...
How come?
 
Old 08-08-2018, 08:12 AM   #8
naveenmajumdar
LQ Newbie
 
Registered: Aug 2018
Posts: 1

Rep: Reputation: Disabled
Unhappy same PS command not giving output while using in cronjob

i am facing same issue where manual executing the script giving the result but when we configure the same script on crontab and giving 2>&1 >> output.log its not working.

Any resolution on this.
******************************
#!/bin/sh -c

###edit the following
service=Runner
email=XXXXXXXXXXXXXXXXXXXXXX
PROCESSID=`ps -elf | grep -v grep | grep Runner | awk '{print $4}'`
###stop editing

host=`hostname -f`

D=`date +%c`
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 )); then
echo "$service is running @ $D"
echo "Utlzation of $service"
ps -p $PROCESSID -o %cpu,%mem
fi
 
Old 08-08-2018, 08:54 AM   #9
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,750

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Same solution: Use absolute path names for commands. cron doesn't have an environment, so it doesn't know where ps, grep, hostname, etc. are.
Use
Code:
which <command>
from the terminal to identify the absolute path for each command.

And, generally speaking, you should start your own thread instead of reopening a 7-year-old thread.
 
Old 08-08-2018, 09:24 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You can also set PATH env variable at top of script
Code:
#!/bin/sh

PATH=/usr/local/bin:/usr/bin:/bin

...
 
Old 08-10-2018, 01:00 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
At the top of your script have
Code:
#!/bin/bash
No options!
For example -l would run your .profile first, that is not meant to be run from cron.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
RHEL 5.4, "crontab" command not found borfgordon Linux - Software 1 11-20-2009 11:30 AM
The "Log out" and "Lock screen" actions cannot be executed through keyboard shortcuts Snood Linux - Desktop 0 04-22-2009 09:30 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
"modprobe usbkbd" command cann't be executed! lucklemon Linux - Hardware 1 05-21-2004 08:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration