Alternative for #reboot cron job, start job when cron daemon starts - linux

I have a script which is specified to start on boot-up with the #reboot annotation.
I tried to restart the script by stopping the cron daemon and starting it by entering service crond stop and service crond start, respectively.
However, I noticed that the script doesn't restart at the restarting of the cron daemon, but only when the entire system is rebooted.
My question is, since the cron daemon starts when the system is booted, is there a way start jobs not on reboot but specifically when the cron daemon starts so that service crond stop and service crond start work as expected?

Unfortunately, there is no way to do so,
Cron daemon just ignores #reboot directive
(CRON) INFO (Skipping #reboot jobs -- not system startup)
However, if you're trying to start some script at boot time and have ability to restart it without rebooting the machine, you might want to consider creating either init script or, if you're using systemd, systemd service description.(same with upstart and other init replacements)

Related

How to disable cron service delay on startup

I'm trying to add the execution of a shell script on server startup. I do it using cron, so I configured it with 'crontab -e' command. It looks so:
#reboot /home/user/run.sh
Then I enabled service with
sudo systemctl enable cron.service
But when I reboot my server, jobs are not started. I check service status with:
sudo systemctl status cron.service
screenshot with an example
And there is a message that it's 2h 55min left to start executing all jobs. So, as I understood service is running but with 3 hours delay. It's happening after every server startup.
Using sudo systemctl restart cron.service command helps to make service working, but the server skips #reboot jobs because "it's not system startup".

Run Script after pause EC2

I need running a (bash)Script after each start from my EC2.
The machine stops 90% a day - after wake up - a script should run.
I tried to push it in the USER-DATA - but this only runs on init.
After this I followed up here: https://aws.amazon.com/de/premiumsupport/knowledge-center/execute-user-data-ec2/
but this didn't also work - because to stop a machine and start a machine seems to be no reboot
I also implement a simple output in the rc.local but also: nothing happens.
Is there a way?
So we talk to switch from this Instance-State
to this:
You could use the oneshot feature of systemd
Write scripts for mystart.sh and mystop.sh, chmod/chown them
/etc/systemd/system/mystart.service. Note that we must specify RemainAfterExit=true so that systemd considers the service as active after the setup action is successfully finished.
[Unit]
Description=mystart
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/mystart.sh
RemainAfterExit=true
ExecStop=/usr/local/bin/mystop.sh
StandardOutput=journal
[Install]
WantedBy=multi-user.target
Reload the systemd (systemctl reload) and try stopping and starting to test it
Cloud-Init (which runs the User Data script on first boot) can also run scripts:
On every boot
On the next boot only
So, simply install your script in this directory:
/var/lib/cloud/scripts/per-boot/
Each time the instance is booted (started), the script will run. This is a great way to trigger a batch process on the instance.
See also: Auto-Stop EC2 instances when they finish a task - DEV Community

Cron daemon not running in debian

I am using Jessie. Cron was working fine last week. But I just checked today and found out, it's not working anymore. When I restart upstart loads my custom tasks. But if I check
sudo service --status-all
Cron is shown marked as [ - ] cron
When I execute sudo service --status-all, I get [FAIL] cron is not running ... failed! in return. Manually starting cron service executes the tasks in crontab. But, the service is not starting on boot.
There is no problem with crontab task specification. They are working fine on my other machines running Ubuntu.

Ubuntu upstart can not stop/restart graphite carbon-cache

I created upstart config file at: /etc/init/carbon-cache.conf to stop/start/restart carbon-cache process. I can start carbon-cache process using command: start carbon-cache, however, I could not use stop/restart carbon-cache and always gives me errors: "stop: Unknown instance:".
Does anyone know what seem to be the issue? Here is the my upstart config: /etc/init/carbon-cache.conf
description "Daemonized Carbon-Cache"
start on runlevel [2345]
stop on runlevel [016]
setuid www-data
setgid www-data
exec /opt/graphite/bin/carbon-cache.py start
respawn
respawn limit 10 5
I suggest using this carbon-cache.conf file: https://gist.github.com/dbeckham/8057390
i think what's happening is that your upstart is successfully able to exec it, but as soon as it runs, carbon-cache, because of it's daemonic nature detaches itself from upstart. So when upstart tries to kill it, it realizes that carbon-cache is no longer attached.
Upstart expects the command run to stay in the foreground, not fork-off and de-attach.
"Twistd, the utility used to daemonize carbon-cache supports a --nodaemon flag that launches the process in the foreground instead of forking it into the background. At the time this article was posted, the only way to get the --nodaemon flag to twistd was by starting carbon-cache with --debug."
Though, i'd advice against un-daemoning carbon, which is necessary in an upstart implementation.
sudo /opt/graphite/bin/carbon-cache.py start

Can I use cron to run long processes or services?

I need to have some processes start when the computer boots and run forever. These are not actually daemons, ie. they do not fork or demonize but they do not exit. I am currently using cron to start them using the #reboot directive like this:
#reboot /path/to/myProcess >>/logs/myProcess.log
Could this cause any problems with the cron daemon? I thought I could try nohup ... & to detach the new process from cron, like this:
#reboot nohup /path/to/myProcess >>/logs/myProcess.log &
Is this required at all?
Is there some other, preferred method to start processes at system boot? I know all Linux distributions provide config files and means to run a program as a service but I am looking for a method that is not Linux distribution specific.
http://www.somacon.com/p38.php
This article answers my question. It suggests that running daemons this way spawns two extra processes, a cron and a shell process, that live for as long as your daemon.
I tested this with linux and following the instructions I was able to get rid of the cron process but not the zombie shell process.

Resources