I have crontab configured like this:
*/2 * * * * php /home/ec2-user/myapp/myscript.php >> /home/ec2-user/myapp/log/myapp.log 2>&1
When I execute ps aux, I see the following output:
ec2-user 1296 0.0 0.0 2984 992 ? Ss 15:36 0:00 /bin/sh -c /home/ec2-user/myapp/myscript.php >> /home/ec2-user/myapp/log/myapp.log 2>&1 SHELL=/bin/sh HOME=/home/ec2-user PATH=/usr/bin:/bin LOGNAME=ec2-user USER=ec2-user
ec2-user 1299 0.3 3.7 91528 63612 ? S 15:36 0:16 /home/ec2-user/myapp/myscript.php SHELL=/bin/sh USER=ec2-user PATH=/usr/bin:/bin PWD=/home/ec2-user SHLVL=1 HOME=/home/ec2-user LOGNAME=ec2-user _=/usr/bin/php
To me it looks like same process was started twice at the same time, process one with PID 1296, process two with PID 1299.
Is that normal? Why two processes are in ps output instead of one?
It looks like /bin/sh is used to invoke your cron tabs, so that they run in their own environment, that's the first process. Then /bin/sh invokes php to run your actual script, and php is your second process. There's nothing wrong with this.
Try to add exec before php
*/2 * * * * exec /usr/bin/php /home/ec2-user/myapp/myscript.php >> /home/ec2-user/myapp/log/myapp.log 2>&1
The shell process /bin/sh will be replaced by exec
Related
I'm trying to run a Docker container every other minute that is stopped via cron job but it seems not working.
What I've done is launch the command crontab -e and add the line
*/1 * * * * docker start sender >> /home/cronlog.log 2>&1
I've added the user group to Docker as explained here (in fact I can access docker from the terminal without sudo)
I have also tried to add the command into a script as below
*/1 * * * * /home/start_container.sh >> /home/cronlog.log 2>&1
with the script containing
#!/bin/sh
docker start sender
but still, nothing happens. The cron process is working tho as using the command ps -ef | grep cron I got
root 881 1 0 08:42 ? 00:00:00 /usr/sbin/cron -f -P
nicola 10905 10178 0 11:31 pts/0 00:00:00 grep --color=auto cron
Am I missing something? (Obviously, the commands work if launched manually from the terminal)
Try using the docker path instead.
type the following command to get the path of docker.
$ where docker
/usr/bin/docker
/bin/docker
then try any one of the paths in the cron script
*/1 * * * * /bin/docker start sender >> /home/cronlog.log 2>&1
or
*/1 * * * * /usr/bin/docker start sender >> /home/cronlog.log 2>&1
It turned out that, for some reason, the cron doesn't like the /home/ (at least, in this specific instance)
I've fixed using another path such as
*/1 * * * * docker start sender >> /tmp/cronlog.log 2>&1
I am trying to use a docker container based on an Alpine image to run a scheduled cron job, following this tutorial, but after printing the statement in my startup script, the container just exits, without running my other script.
My docker-compose service is configured as follows:
cron:
image: alpine:3.11
command: /usr/local/startup.sh && crond -f -l 8
volumes:
- ./cron_tasks_folder/1min:/etc/periodic/1min/:ro
- ./cron_tasks_folder/15min:/etc/periodic/15min/:ro
- ./cron_tasks_folder/hourly:/etc/periodic/hourly/:ro
- ./scripts/startup.sh:/usr/local/startup.sh:ro
So it runs an initial script called startup.sh and then starts the cron daemon. The startup.sh script contains the following:
#!/bin/sh
echo "Starting startup.sh.."
echo "* * * * * run-parts /etc/periodic/1min" >> /etc/crontabs/root
crontab -l
sleep 300
I dropped a sleep command in there just so I could launch an interactive shell on the container and make sure everything inside it looks good. The script creates another folder for 1min scripts. I have added a test script in there, and I can verify it's there:
/etc/periodic/1min # ls -a
. .. testScript
The script is executable:
/etc/periodic/1min # ls -l testScript
-rwxr-xr-x 1 root root 31 Jul 30 01:51 testScript
And testScript is just an echo statement to make sure it's working first:
echo "The donkey is in charge"
And looking at the root file in etc/crontabs, I see the following (I've re-run the container several times, and each time it's creating a new 1min folder, which is unnecessary, but I think not the problem here):
# do daily/weekly/monthly maintenance
# min hour day month weekday command
*/15 * * * * run-parts /etc/periodic/15min
0 * * * * run-parts /etc/periodic/hourly
0 2 * * * run-parts /etc/periodic/daily
0 3 * * 6 run-parts /etc/periodic/weekly
0 5 1 * * run-parts /etc/periodic/monthly
* * * * * run-parts /etc/periodic/1min
* * * * * run-parts /etc/periodic/1min
* * * * * run-parts /etc/periodic/1min
* * * * * run-parts /etc/periodic/1min
* * * * * run-parts /etc/periodic/1min
The echo statement in testScript is never printed to my terminal, and the container exits with exit code 0 shortly after starting. I want to print this statement every minute... what am I missing?
In the docker compose file you have
command: /usr/local/startup.sh && crond -f -l 8
The intention is to run as a shell command, but it's not at all clear from the question that's what's going to happen; that depends on your ENTRYPOINT. Since it's defined with [] brackets, not additional shell will be provided. The command value will be passed as arguments to the ENTRYPOINT.
Assuming that will become a shell command, && in the shell runs the left hand side, and if that succeeds, then runs the right hand side. So startup.sh needs to complete before crond is executed. startup.sh ends with
sleep 300
crond is invoked only after that 300 seconds.
In either case, crond is either not invoked at all, or sleep has not been completing. The comments show that an error starting crond was discovered.
Using an entrypoint such as this is standard practice to configure the environment before, or provide runtime parameters when, invoking the main executable. To do it right, you should make sure to use exec to run the main executable so that it receives the signals that would otherwise go to the bash shell running the entrypoint script.
So at the end of startup.sh:
exec crond -f -l 8
Will replace the shell running startup.sh with crond, so that crond receives all signals (at this point the shell is gone). It's subtle but important!
In general, keep the invocation of the application as simple as possible. Case in point, your execution process was split between entrypoint, command, and startup script, with no clear interface between them. You wouldn't have gotten hung up on the invocation if you had put crond directly into the Dockerfile and left it at that. Sometimes arguments must be provided at runtime, but environment variables - which have names, not just positions - are often preferred. This keeps invocations simple and debugging straightforward. But, when that doesn't work, a shell script entrypoint is a great solution - just make sure to exec your final process!
This question may be duplicate (CronJob not running) however, it does not resolve my issue. Let me explain this. By seeing the post in https://laravelcode.com/post/laravel-55-task-scheduling-with-cron-job-example , I wanted to set the cronjob via terminal. I see from FTP that artisan has set in the path: /var/www/html/projectName/
and I believe that this server is Ubuntu. I ran this below command but did not happen anything.
* * * * * /var/www/html /var/www/html/projectName/artisan schedule:run 1>> /dev/null 2>&1
From the above mentioned stackoverflow post, I ran the command ps ax | grep cron which return me:
1013 ? Ss 0:57 /usr/sbin/cron -f
19067 pts/0 S+ 0:00 grep --color=auto cron
I believe the path of cronjob is set as wrong. But how can I rectify this, I can't understand. Any help is appreciated.
I run this command and it works well
* * * * * php /var/www/html/projectName/artisan schedule:run >> /dev/null 2>&1
From: http://www.expertphp.in/article/laravel-5-task-scheduling-with-cron-job-example
did someone use crontab on juniper swith EX series? I would like to do restart httpd job, because I need to run this process with different config.
I create some simple script with logging.
#!/bin/csh
echo 'Go...'
cp /jail/var/etc/httpd.conf_new /jail/var/etc/httpd.conf
echo 'changed'
set http_pid=`ps aux | grep 'httpd --config' | grep -v grep | awk '{print $2}'`
echo 'Process PID: ' "$http_pid"
kill -9 "$http_pid"
Also I create a job in crontab
10 12 * * * csh /var/root/test.sh >> test.log
When I run proces from cmd then output is:
Go...
changed
Process PID: 3158
And everything is ok but when I run it from cron then it looks like that:
Go...
changed
Process PID:
I try to change(add) to the crontab line with:
SHELL=/bin/csh
PATH=...
but didn't work. Also I try to change line with job to something like below:
10 12 * * * /var/root/.profile; csh /var/root/test.sh >> test.log
10 12 * * * sh /var/root/.profile; csh /var/root/test.sh >> test.log
10 12 * * * (sh /var/root/.profile; csh /var/root/test.sh) >> test.log
Maybe you know what can I do more to run this correctly?
I resolved this over the weekend. When the script is run via crontab using #reboot, the script runs before httpd is running, therefore there is no PID. I put a sleep 60 in my crontab line and the script delayed for 60 seconds, when it runs, httpd is up and running and has a PID.
Here is my Crontab:
#reboot sleep 60; sh /jail/var/etc/httpd_replace_config.sh > /jail/var/etc/httpd_replace_config.txt
At first, this is not exactly a programming question and more specific to Linux. I hope that it can be answered here though.
I have created a cron job which will execute a shell script every 2 minutes on my machine. However, the cronjob is not executing.
output of crontab -e command
2 * * * * /home/yuri/connector.sh >> /home/yuri/test.txt 2>&1
I have the cron daemon running:
ps aux | grep cron
root 944 0.0 0.0 19120 932 ? Ss 08:25 0:02 cron
1000 19619 0.0 0.0 13600 880 pts/2 S+ 21:50 0:00 grep --color=auto cron
The connector.sh shell script runs properly when I execute it manually, however when run through the cron job created above, it does not work.
I have redirected the output to a text file to know if something is going wrong while executing the cron job, but no such text file is created.
Your cron job isn't set to run every two minutes, it's set to run on the second minute of every hour. You might change it as follows:
*/2 * * * * /home/yuri/connector.sh >> /home/yuri/test.txt 2>&1
Running every 2 minutes should be given as
*/2 * * * * /home/yuri/connector.sh >> /home/yuri/test.txt 2>&1.
Does this work ?