Upstart - Job starts but then stops immediately - php

I am trying to set up an upstart job but for some reason it stops immediately after starting
I'm on Cents 6.5 btw
here are my files:
/ect/init/test-daemon.conf
start on startup
stop on shutdown
respawn
script
sudo -u root php -f /usr/share/test_daemon.php
end script
/usr/share/test-daemon.php
<?php
// The worker will execute every X seconds:
$seconds = 2;
// We work out the micro seconds ready to be used by the 'usleep' function.
$micro = $seconds * 1000000;
while(true){
// Now before we 'cycle' again, we'll sleep for a bit...
usleep($micro);
}
I have then got this file: (which I found on a forum that logs events)
/tmp/log.file
debug/ (/dev/fd/10):19735:Fri Jul 25 11:52:40 AST 2014:Job
test-daemon/ starting. Environment was: TERM=linux
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
PWD=/ JOB=test-daemon
SHLVL=1
UPSTART_INSTANCE=
UPSTART_EVENTS=starting
UPSTART_JOB=debug
INSTANCE=
_=/usr/bin/env
debug/ (/dev/fd/9):19775:Fri Jul 25 11:52:41 AST 2014:Job test-daemon/ stopping. Environment was:
TERM=linux
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
PWD=/
JOB=test-daemon
RESULT=ok
SHLVL=1
UPSTART_INSTANCE=
UPSTART_EVENTS=stopping
UPSTART_JOB=debug
INSTANCE=
_=/usr/bin/env
debug/ (/dev/fd/9):19779:Fri Jul 25 11:52:41 AST 2014:Job test-daemon/ stopping. Environment was:
TERM=linux
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
PWD=/
JOB=test-daemon
RESULT=failed
SHLVL=1
PROCESS=respawn
UPSTART_INSTANCE=
UPSTART_EVENTS=stopping
UPSTART_JOB=debug
INSTANCE=
_=/usr/bin/env
in the log file, I get more debug information but its basically the above repeated a few times.
I get this from running start test-daemon which outputs test-daemon start/running, process 20600
I know the test-daemon.php doesn't actually do anything...at the moment I just need to get the actual job running, once thats fixed ill drop in my code
So from the above...is there anything I am doing wrong? as the job should only stop if I run stop test-daemon right?
Any suggestions would be much appreciated :)
Thanks,
Dave

I'd rather post a comment but with the low reputation limit I can not.
I am not familiar with this distro flavor but did you try to use nohup ?
Like this:
nohup php -f /usr/share/test_daemon.php &
It could be possible that detaching your command from the service process kills it.

Related

Script runned by crontab will no produce data

Using Cacti, I'm trying to run the following command:
php poller.php > test.cfg
As expected I'm getting the following output :
root#SLINUX01:[/usr/local/cacti]$tail -f test.cfg
Waiting on 1 of 1 pollers.
Waiting on 1 of 1 pollers.
Waiting on 1 of 1 pollers.
Waiting on 1 of 1 pollers.
05/12/2016 01:23:50 PM - POLLER: Poller[0] CACTI2RRD: /usr/bin/rrdtool update /usr/local/cacti/rra/stse01_traffic_in_14.rrd --template traffic_out:traffic_in 1463052230:2773678063:3900704413
05/12/2016 01:23:50 PM - POLLER: Poller[0] CACTI2RRD: /usr/bin/rrdtool update /usr/local/cacti/rra/stse01_traffic_in_15.rrd --template traffic_in:traffic_out 1463052230:1071486:1088742
05/12/2016 01:23:50 PM - SYSTEM STATS: Time:5.4128 Method:cmd.php Processes:1 Threads:N/A Hosts:3 HostsPerProcess:3 DataSources:9 RRDsProcessed:7
Loop Time is: 5.41
Sleep Time is: 294.58
Total Time is: 5.42
^C
And my manipulated has updated.
Now using the crontab, I made the following :
*/5 * * * * cacti php /usr/local/cacti/poller.php > /home/cacti/test.cfg &>/home/cacti/test.cfg
But when i go in the /home/cacti/test.cfg after cron execution , I got nothing, but the cron runned the script :
May 12 15:25:01 SLINUX01 CROND[19871]: (root) CMD (cacti php /usr/local/cacti/poller.php > /home/cacti/test.cfg &>/home/cacti/test.cfg)
What is the source of this problem?
Thanks in advance.
Your cron log entry only says that the command was tried. It does not guarantee that the command has been successfully started (permission problems? environment variables unset? unmounted filesystem?).
Do you use the cron table for the same user that can successfully run the command from command-line?
What are the exact paths when you run from command line?
Note that in crontab you try to invoke php without giving its full path - that may well be the culprit.

Magento 1.9.1 Cron Not Running

I am having big issues with the cron of Magento 1.9.1 hoping someone can help. I have AOE installed and see not heartbeat.
I have added my cron job
*/5 * * * * /bin/sh /var/sites/p/domain/public_html/cron.sh
But this still does not run, so I decided to try and try and run cron.php, this does nothing. This is my cron for this
*/5 * * * */usr/bin/wget -O /dev/null -o /dev/null http://www.domain.com/cron.php
Based on Magneto 1.8 cron.php I added the following to the cron.php.
$isShellDisabled = true;
Based on this still not working I tried the cron.php in the browser. I get an error in the browser that Printing is disabled by default for security reasons.
So I changed my php.ini to memory_limit = 512M just incase. This is still not working.
The error I get in my reports for this is:
a:5:{i:0;s:59:"Mage registry key "_singleton/cron/observer" already exists";i:1;s:537:"#0 /var/sites/p/domain.co.uk/public_html/app/Mage.php(223): Mage::throwException('Mage registry k...')
#1 /var/sites/p/domain.co.uk/public_html/app/Mage.php(477): Mage::register('_singleton/cron...', false)
#2 /var/sites/p/domain.co.uk/public_html/app/code/core/Mage/Core/Model/App.php(1316): Mage::getSingleton('cron/observer')
#3 /var/sites/p/domain.co.uk/public_html/app/Mage.php(448): Mage_Core_Model_App->dispatchEvent('default', Array)
#4 /var/sites/p/domain.co.uk/public_html/cron.php(74): Mage::dispatchEvent('default')
#5 {main}";s:3:"url";s:9:"/cron.php";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:5:"admin";}
Any help would be really great.
Note ** I have change the domain name to DOMAIN due to my site not being suitable for work.
Few simple steps to fix Magento cron issue:
Go to System > Configuration > Advanced > System > Cron (Scheduled Tasks) and set next values:
Generate Schedules Every: 15
Schedule Ahead for: 20
Missed if Not Run Within: 15
History Cleanup Every: 10
Success History Lifetime: 60
Failure History Lifetime: 600
Clear Cache in System > Cache Management
Clear 'cron_schedule' table. You can do it via phpmyadmin or execute next command in SQL console:
TRUNCATE TABLE cron_schedule;
Add next line to cron settings (use absolute paths):
*/5 * * * * /usr/bin/php /absolute/path/on/your/server/to/file/cron.php
Note: in some configurations on webserver (apache/nginx) allowed to execute php scripts. In this case add this line to webserver's user crontab. Example for apache: crontab -u apache -e
Restart cron daemon.
Check table 'cron_schedule', if it's not empty - Magento cron script start working.
Well I've had a different issue. My cron was simply not working, although cron.php was executed properly. Still no output, and no jobs done.
I've checked cron_schedule and noticed that last job there is from 19th of March this year (sic!). So i kept on looking for cause.
Finally I've noticed that that there is process running with my cronjob from 19th of March (sic!).
josh 21350 99.6 0.4 350400 133492 ? R Mar19 241812:21 /usr/bin/php /var/www/mysite/cron.php -mdefault
I've killed the job with: kill -9 21350
And voila! Suddenly newsletter is being sent!
I had no idea why, and how this happened. Now i know i should pay attention to it, so I will.
I just saw that you are using magento 1.9.1.
Run the cron using php instead : /usr/local/bin/php -f /home/cpanel_username/public_html/cron.php
This should resolve http permission problems you might be having when using 'usr/bin/wget -O /dev/null -o /dev/null'
Try that command and let me know if it works for you.

Running php script in the background

I have this simple sample script:
#!/usr/bin/php
<?php
while(true) {
error_log("hello " . time() . "\n", 3, "logs.log");
sleep(3);
}
Which I execute with the command:
me:~/Desktop$ php worker.php &
As expected it returns me 6683: the id of the process. Fine.
Now to make sure everything is fine I do a ps and get:
PID TTY TIME CMD
5561 pts/1 00:00:00 bash
6683 pts/1 00:00:00 php
6705 pts/1 00:00:00 ps
[1]+ Stopped php worker.php
Why is it stopped? Is it not supposed to run continuously untill someone kill -9 it?
If I do a second ps I get:
PID TTY TIME CMD
5561 pts/1 00:00:00 bash
6683 pts/1 00:00:00 php
7395 pts/1 00:00:00 ps
The process is there but it is not logging (dormant?)
Anyone?
Ta
The script must have a place where to dump stdout or else it shows the above behaviour (gets stopped). This can be done like this:
php worker.php > /dev/null &
Like this it works and the timed infinte loop does its job. The only problem is: if you now stop the parent process (the shell's) then the background php process stops too.
To get around it run:
disown -h php-process-id
Youi can now close all the shells, log out and go to the pub.
Well done me! ;)
Not sure if my reasoning is correct, but I've run into this problem, too, and solved it by using nohup.
My reasoning was this:
A PHP process has a handle on stdin. By running a process on the background, you can't read from the stdin, perhaps at some point a hangup signal might be sent.
By using nohup, you can ignore that signal.
Here's a few extra words of explanation on the matter

Schedule Automatic Server Maintenance [Ubuntu]

I'm running an Ubuntu 12.10 server as a LAMP stack. I access this through a SSH terminal (as I don't have access to the physical box).
My question is, is there a way to setup ubuntu to automatically execute a script at chosen times. Like to schedule automatic maintenance scripts that have been built in PHP or really any language (possibly Bash Scripts?). I'm sure this has to be possible.
Since I am a bit newer to linux/ubuntu, any help would be appreciated.
EDIT:
This is one solution I found if anybody else stumbles across this...
sudo crontab -e
...
#daily /usr/bin/wget -q -O /var/log/maintenence.txt /var/www/admin/script.php
You can use the crontab in linux.. That wil suit your purpose.
Cron is a daemon that executes scheduled commands
eg:
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * 0,6 /usr/bin/find
Okay for example.. if you need to run the script file /home/krizna/backup.sh every sunday 12 PM .. just issue the below command
sudo crontab -e
and add this line at the end of the file
# Minute Hour Day of Month Month Day of Week Command
00 12 * * 7 /bin/sh /home/krizna/backup.sh

How to find the error which is causing a segmentation fault in a script run by cron job

I have a cron job that executes once per day:
0 20 * * * /usr/bin/wget --timeout=10800 -O /home/File.txt http://www.site.com/script.php
and so far all was fine, but recently I found out the script just stops.
I looked in the /var/log/httpd/error_log (it's a CentOS) and found this:
[Thu Nov 21 21:30:32 2012] [notice] child pid 8985 exit signal Segmentation fault (11)
Now, this means two things: the script was successfully running for about hour and a half, but then the segmentation fault happened. Ususally the script takes around 2 hours to complete, so it lacks around half an hour to complete it's job.
Now, I can't find the error which causes the script to stop even though I do have error_reporting turned on.
I'm wondering, is there some way so that I can find a potential error line which caused the script to terminate?
I did try google, and SO, ofc, and tried to achieve the same as on this question here on SO by doing this:
0 20 * * * /usr/bin/wget --timeout=10800 -O /home/File.txt http://www.site.com/script.php 2>1& >> /home/log.txt
but the log file is empty. I'm not so good at managing linux so it may be that my command is wrong in this cronjob, so please steer me right.
Try this
0 20 * * * /usr/bin/wget --timeout=10800 \
-O /home/File.txt http://www.site.com/script.php >> /home/log.txt 2>&1
the 2>&1 part tells to send the STDERR to the same place as STDOUT.
Cron sends the output of the command to the email of the user which is executing it.
For this system to work you must have a mail daemon installed, such as exim or postfix.
Also, you are asking wget to write a file in the /home/ directory, i presume as non-root, which means you can't do that. And if you are root you shouldn't do that.

Categories