Can I use cron to run long processes or services? - cron

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.

Related

The subtle difference in an ansible script "SHELL" and the directly running Shell commands on an ubuntu machine 20.04 EC2?

I come with a Vagrant script that pulls down an bionic 18 which then runs an ansible playbook which generates two EC2s ubuntu 20.04 which successfully run though every "task:" I assign. I am able to run everything I want in a largely automated download and execution for a publisher subscriber method. Here is the issue: I can run my .sh and .py scripts manually and the systems works, but when I use the ansible methods I must be doing something wrong much like these solutions point to:
Shell command works on direct hosts but fail on Ansible
ansible run command on remote host in background
https://superuser.com/questions/870871/run-a-remote-script-application-in-detached-mode-in-ansible
What I want to do is simply correct the issue with this, and run it in the background.
- name: Start Zookeeper
shell: sudo /usr/local/kafka-server/bin/zookeeper-server-start.sh /usr/local/kafka-server/config/zookeeper.properties </dev/null >/dev/null 2>&1 &
- name: Sleep for 30 seconds and continue with play
wait_for:
timeout: 15
- name: Start Kafka broker
shell: sudo /usr/local/kafka-server/bin/kafka-server-start.sh /home/ubuntu/usr/local/kafka-server/config/server.properties </dev/null >/dev/null 2>&1 &
I have tried it with just a single "&" at the end as well as passing in explicit calls to my user account "ubuntu". I've used the "become: yes". I really don't want to use a daemon especially since others seem to have used this successfully before.
I do want to note that a glaring sign to you that I can't seem to think through is that it hangs when I don't include the &, but if I do include the & it just outright fails, which made me think it was running, but the script won't proceed because these are listener processes.
# - name: Start Zookeeper
# become: yes
# script: /usr/local/kafka-server/bin/zookeeper-server-start.sh
# args:
# chdir: /usr/local/kafka-server/config/zookeeper.properties
This failed, and I'd rather not create another script to copy over the directories and localize it if there is a simple solution to the first block of code.
Multiple ways to skin this cat, but I'd rather just have my mistake on the shell ansible command fixed, and I don't see it.
As explained in the answers written in your third link:
https://superuser.com/questions/870871/run-a-remote-script-application-in-detached-mode-in-ansible
This happens because the script process is a child process from the shell spawned by ansible. To keep the process running after ansible has finished you would need to disown this child process.
The proper way to do this is configuring the software (zookeeper in your case) as a service. There are plenty of examples for this such as:
https://askubuntu.com/questions/979498/how-to-start-a-zookeeper-daemon-after-booting-under-specific-user-in-ubuntu-serv
Once you have configured it as a service, you can start it or stop it using ansible service module.

Stop a Linux Script with another Script

I guys, I have a problem, I had been configure some basic script to send files to AWS in windows to backup the files for 8 hours with the task scheduler but now I have to do it in Linux (Centos y Ubuntu), the script is basically this " aws sync "PC folder" "AWS bucket" "and is launched with crontab but how can I run this script only for 8 hours then make it stop it automatically, How can i do this? please Help
If it's the only aws process running on the machine, it could be as simple as writing a script that runs
pkill aws
and schedule it 8 hours after the start time.
Note this searches for any process named aws and kills it, so if other aws commands are running they would be killed too. Killing by process name isn't the most reliable, usually killing by PID is better but I would need to know more details about your script to know how to get the PID, and where to store it.

Trying to write a shell script to monitor when a service stops in linux, and to automate the restart of this service

So I am relatively new to Centos, version 6.2. I have a service that needs to be mnonitored as a cron job, and if it stops needs to be restarted. I have a few ideas on how to monitor it, but when it comes to getting it restarted thats when I get stuck. I also know the PiD of the service I want to monitor.
You can use supervise for this: http://cr.yp.to/daemontools/supervise.html
Put it in your crontab to launch on system start:
#reboot supervise foo

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

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)

.Net Window service equivalent in linux?

Can we hook to similar start,stop etc events. Do we have to write them as shell scripts? I know of mono port of .NET.
You are looking for something called an 'init script'. These are scripts that allow you to start or stop a service with a single command, like so:
service httpd restart
service httpd stop
service httpd start
Some Linux distributions do not include the service command, in which case you access init scripts directly by their location, /etc/init.d, like so.
/etc/init.d/mysqld restart
You can program your init script to accept whatever parameters you want (start, stop, restart, etc). Some basic tutorials on writing init scripts to get you started can be found at the following web pages:
http://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html
http://www.linuxquestions.org/questions/programming-9/how-to-write-init-script-376302/
Many times an init script is unnecessary, and you can just go with the simpler option of executing your program in the background and killing it manually. Running an executable on Linux in the background can be done like so:
./some_prog arg1 arg2 &
And killing it is done like this:
kill `pgrep some_prog`
If you are fairly new to Linux, that latter option might be a much easier way to go until you get a handle on init scripts and the general Linux service ecosystem.

Resources