Scheduling an UpStart init script via crontab - linux

I have created following ProcessRunner.conf in /etc/init/ Ubuntu.
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [016]
# Automatically restart process if crashed
respawn
# Essentially lets upstart know the process will detach itself to the background
expect fork
# Run before process
pre-start script
[ -d /var/run/ProcessRunner ] || mkdir -p /var/run/ProcessRunner
java -Dlog4j.configuration=log4j_process1.xml -classpath /home/devuser/apps/ProcessExecutor:/home/devuser/apps/ProcessExecutor/ProcessExecutor-1.0.jar com.process.ApplicationStartup &
echo $! > /var/run/ProcessRunner/ProcessRunner.pid;
end script
post-stop script
processid=$(cat /var/run/ProcessRunner/ProcessRunner.pid);
if ps -p $processid > /dev/null
then
sudo kill -9 $processid;
fi;
end script
I use following commands to start / stop this from command line:
sudo start ProcessRunner
sudo stop ProcessRunner
It works fine. Now I need to schedule these. I doing following to do so:
I use following commands to start / stop this from command line:
$sudo crontab -e
0 0 * * * * start ProcessRunner
0 2 * * * * stop ProcessRunner
But this is not working. Please help.
Also, I do not want this process to get started on system start up. How can I configure that ?

Crontab environment doesn't provide path, so commands (start ProcessRunner) must include full path.
Full path to commands "start" and "stop", not to your upstart config file!
So, essentially, your crontab shall look like:
0 0 * * * * /sbin/start ProcessRunner
0 2 * * * * /sbin/stop ProcessRunner

Related

Cron won't execute none of my commands on Ubuntu 21.10 impish

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

Docker cron scheduled job not running

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!

Script file restart Tomcat runs manually success, but fails on Crontab

I'm newbie to shell scripting.
I have a Tomcat server build on : /APP/apache-tomcat-7.0.42
I want my tomcat automatic restart one time per day, so I write a file test.sh (/APP/apache-tomcat-7.0.42/test.sh) with content :
/APP/apache-tomcat-7.0.42/bin/shutdown.sh && echo "Tomcat was already shutdown"
kill -9 $(lsof -t -i:8080 -sTCP:LISTEN)
/APP/apache-tomcat-7.0.42/bin/startup.sh
And I install on crontab: 0 9 * * * /APP/apache-tomcat-7.0.42/test.sh
But not working, although I try run manually, and success.
I checked crontab : /etc/init.d/crond status, it is running.
I dont understand, help me!
Oh, I resloved!
If you can do it manually, from a log in session, but not automatically from
startup or from cron, I'm 99% sure it's because environment variables like
JAVA_HOME and CATALINA_HOME are not being set for the startup and cron environments.
You need get info in this session:
> echo $JAVA_HOME
> JAVA_HOME="/usr/java/jdk1.6.0_41"
> echo $CATALINA_HOME
> CATALINA_HOME="/APP/apache-tomcat-7.0.42"
Then, Result file crontab:
export PATH="/usr/lib64/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/tomcat/bin"
export JAVA_HOME="/usr/java/jdk1.6.0_41"
export CATALINA_HOME="/APP/apache-tomcat-7.0.42"
/APP/apache-tomcat-7.0.42/bin/shutdown.sh
kill -9 $(lsof -t -i:8080 -sTCP:LISTEN)
/APP/apache-tomcat-7.0.42/bin/startup.sh

Cron job break other foreground server in docker

In order to start selenium server on start,
I have to put entry_point.sh under /opt/bin
but if I uncomment the cron command CMD cron && tail -f /var/log/cron.log
The server won't run anymore.
I couldn't figure out why it will be broken
This is my cron file
SHELL=/usr/bin/zsh
* * * * * echo $PATH >> /root//reboot_record.txt >> /tmp/cron_debug_log.log 2>&1
* * * * * cd /home/poc/lazy-bird/j && ruby j.rb >> /tmp/cron_debug_log.log 2>&1
* * * * * echo $PATH >> /root//reboot_record.txt >> /tmp/cron_debug_log.log 2>&1
* * * * * cd /home/poc/lazy-bird/p && ruby p.rb >> /tmp/cron_debug_log.log 2>&1
This is my docker file
#====================================
# Scripts to run cron job
#====================================
# Add crontab file in the cron directory
ADD cron_lazy_bird /etc/crontab
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Use the crontab file
RUN crontab /etc/crontab
# Run the command on container startup
#CMD cron && tail -f /var/log/cron.log
#====================================
# Scripts to run Selenium Standalone
#====================================
COPY entry_point.sh /opt/bin/entry_point.sh
RUN chmod +x /opt/bin/entry_point.sh
USER seluser
EXPOSE 4444
USER root
CMD ["/bin/bash", "/opt/bin/entry_point.sh"]
UPDATE entry_point.sh
cron && tail -f /var/log/cron.log
export GEOMETRY="$SCREEN_WIDTH""x""$SCREEN_HEIGHT""x""$SCREEN_DEPTH"
xvfb-run --server-args="$DISPLAY -screen 0 $GEOMETRY -ac +extension RANDR" \
java -jar /opt/selenium/selenium-server-standalone.jar \
-role node \
-hub http://$HUB_1_PORT_4444_TCP_ADDR:$HUB_1_PORT_4444_TCP_PORT/grid/register \
-browser browserName=firefox
Your entrypoint script needs to be called from either the ENTRYPOINT or CMD instruction, or it won't get run when the container is started.
You'll need to remove the CMD and start cron from the entrypoint script before launching the application. You might want to use a process manager such as runit or supervisord to handle this (see https://docs.docker.com/articles/using_supervisord/).

task in crontab does not work

I want to automatically start a program if it crashes (check if the process exists, if not, restart it), so using crontab is a simple solution, but
I creatd a simple crontab task using crontab -e or in /etc/crontab file like this:
* * * * * /usr/bin/gnome-terminal
it doesn't work, it never launches gnome-terminal, I can see it executes the task in /var/log/syslog (Ubuntu) or /var/log/cron (CentOS), the gnome-terminal never comes up.
if you say since gnome-terminal is a gui program, then, this script to create bluetooth service won't work either :
using crontab -e or /etc/crontab:
* * * * * /home/username/run-bt
run-bt:
#!/bin/sh
# find bt pid
/usr/bin/pgrep bluetoothd
# if not running
if [ $? -ne 0 ]
then
/usr/sbin/service bluetooth restart
fi⏎
I already made the run-bt script executable, but bluetooth service won't start even if I stop the bluetooth service manually.
but this:
* * * * * /bin/ls > /tmp/ls.output
works as expected.
What's wrong?
Actually crontab does not load all the environment variables of the current user it runs for, just a few of them.
Maybe some of those environment variables are required by the scripts you try to run?

Resources