Enable/Disable tasks in Crontab by Bash/Shell - linux

Is there a way to enable and disable Crontab tasks using Bash/Shell?
So when the user starts Server 1, it will enable the Server 1 Crontab line and so on.
And when the user stops Server 1, the Server 1 Crontab line get disabled (#).
Is this possible and how?
Thanks in advance
*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check

SERVERNUM=$1
To enable:
crontab -l | sed "/^#.*Server $SERVERNUM check/s/^#//" | crontab -
To disable:
crontab -l | sed "/^[^#].*Server $SERVERNUM check/s/^/#/" | crontab -
Transcript:
barmar#dev$ crontab -l
*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
barmar#dev$ crontab -l | sed '/^[^#].*Server 1 check/s/^/#/' | crontab -
barmar#dev$ crontab -l
#*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check
barmar#dev$ crontab -l | sed '/^#.*Server 1 check/s/^#//' | crontab -
barmar#dev$ crontab -l
*/1 * * * * Server 1 check
*/1 * * * * Server 2 check
*/1 * * * * Server 3 check

I suggest you add your cron jobs to /etc/cron.d for every server one script. Then let the cron script scan for some marker file if the cron job should be executed.

As a quick and dirty fix, you can enable or disable the execute permission of the appropriate cron script.
E.g. if you like to prevent locate from automatically updating its database (which can be I/O consuming):
cd /etc/cron.daily
sudo chmod a-x locate
This may be against the cron framework, but it is quickly applied and it works in case of immediate needs.

this is a variant, I use a cronjob that loads it self every night. I just edit a file and it gets reloaded at 10pm everynight. You could make the reload happen more often. I keep a directory of files for each of nodes. The trick is make sure that nobody comments out the reload line.
0 22 * * * crontab /home/ME/cron_files/NODE

Related

Doesn't run this commands on mautic cron job

Hi guys i have the next problem(sorry for my english):
I want to executate a command in Mautic cron job, i put the next comands:
*/1 * * * * /usr/local/bin/php /apps/mautic/htdocs/app/console mautic:segments:update
*/1 * * * * /usr/local/bin/php /apps/mautic/htdocs/app/console mautic:campaigns:update
*/1 * * * * /usr/local/bin/php /apps/mautic/htdocs/app/console mautic:campaigns:trigger
I try a lot of things but no one of them work, like:
*/1 * * * * root /usr/local/bin/php /apps/mautic/htdocs/app/console mautic:segments:update
or
*/1 * * * * bitnami php /apps/mautic/htdocs/app/console mautic:segments:update
But i don't know what fail's,if it's the user name,the route to php,the comand,if they doesn't have permission...
If i put this manually work perfectly
php /apps/mautic/htdocs/app/console mautic:segments:update
ty so much
You should check whether the path of the php program correct. You can check the the full path of the php program in terminal output by this command:
which php
To create a cron job, should always use the full path of the program. The crontab default environment is not like the logged in user. The program may not be found if the path is not defined to the crontab environment.
The crontab syntax composed of two parts, datetime to execute & command to be executed. User is not required to add before the command.
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
I had the same problem, it turns out that my root user wasn't enabled, therefore there wasn't any way for the system to log in with a root account to perform the cronjobs.
I just made sure that I had my root account enabled with a valid pasword.
Check that by typing:
sudo passwd --status root
If this is the case, just try to change your root password
sudo passwd root

Why is my crontab not working?

I'm trying to get a cron job to run rsync. I started with this:
*/30 * * * * rsync -avz -e "ssh -i /home/ubuntu/ocf_dev_us" ubuntu#10.0.12.76:/home/ubuntu/kumar/ /home/ubuntu/kumar/"
and it wasn't working, so I replaced it with this:
0 * * * * env > /tmp/env.output
and even that isn't working. How do I find out what's going on?
Your cronjob fires only every 60 minutes. Try this
* * * * * env > /tmp/env.output
to find out if your cronjob is working.

Can't make crontab work

I am new to Linux and Ubuntu and I seldom have to use it. I am trying to make this PHP script to run every minute using cron, but firstly I wanted to make some tests.
I created an empty file at /var/www/html/ called test. I ran on terminal:
sudo crontab -e
And added this line:
0 * * * * rm /var/www/html/test
Then saved it and exited. It said "Installing new Crontab"
Nothing happened. Then I created a file bfile.sh that contained:
#!/bin/sh
rm /var/www/html/test
and added the following to crontab:
0 * * * * bash /var/www/html/bfile.sh
Still nothing happened.
What do I have to do to see anything happening from crontab? By the way I checked and the service is running
0 * * * * basically says "run this at 0th minute of every hour."
If you need cron to run your command every minute do * * * * *.
0 * * * * runs once every 1 hour. If you want to run every minute it should be */1 * * * *
You can also check the /var/log/cron file for any errors

crontab not running command

I've been trying to run a crontab command but it isn't running for some reason. The command is supposed to send push notifications.
My sudo crontab -e looks like this:
0 0 * * 0 /home/[user]/resetWeeklyLeaderboard
* * * * * /home/[user]/pushDelivery
I have a file called pushDelivery at the location above which contains the following:
/usr/bin/flock -n /home/[user]/PushChatServer/push/lockfile usr/bin/php /home/[user]/PushChatServer/push/push.php development
I have also made pushDelivery executable by doing chmod +x pushDelivery. This code works perfectly for my resetWeeklyLeaderboard file but won't call the pushDelivery file.
It works if I run
/home/[user]/pushDelivery
It works if I run
/usr/bin/flock -n /home/[user]/PushChatServer/push/lockfile usr/bin/php /home/[user]/PushChatServer/push/push.php development
However if in crontab I do
* * * * * /home/[user]/pushDelivery
or
* * * * * /usr/bin/flock -n /home/[user]/PushChatServer/push/lockfile usr/bin/php /home/[user]/PushChatServer/push/push.php development
it doesn't work. Please help me. Thank you!
UPDATE:
It still doesn't work but I've tried more stuff. I tried reversing the order in the crontab
* * * * * /home/[user]/pushDelivery
0 0 * * 0 /home/[user]/resetWeeklyLeaderboard
it doesn't work. I also tried making my resetWeeklyLeaderboard code run minutely
* * * * * /home/[user]/pushDelivery
* * * * * /home/[user]/resetWeeklyLeaderboard
and that works for my resetWeeklyLeaderboard code but not for my pushDelivery code. This implies to me that it is something in my pushDelivery code that is causing the issue. However I run /home/[user]/pushDelivery from command line and it works. What could be causing this problem?
i dont know why is is not running but same thing happens to me before at that time insted of doing
* * * * * /home/[user]/pushDelivery
try this
*/1 * * * * /home/[user]/pushDelivery
it worked for me in this way...(both the task run in every minute)
I fixed the issue. In my pushDelivery file I was supposed to write:
/usr/bin/flock -n /home/[user]/PushChatServer/push/lockfile /usr/bin/php /home/[user]/PushChatServer/push/push.php development
whereas I'd written:
/usr/bin/flock -n /home/[user]/PushChatServer/push/lockfile usr/bin/php /home/[user]/PushChatServer/push/push.php development
The "/" before the "usr/bin/php" makes all the difference. Somehow just that slash will allow it to work outside of crontab but will fail it when run inside crontab. I don't understand why but this is the correct solution.

cron job in crontab not working

I have added the following entry:
*/1 * * * * /home/coddict/myapp-dev/spoolemailsender
and the shell that I am trying to execute (the file spoolemailsender) has the following:
#!/bin/sh
php app/console swiftmailer:spool:send --env=dev
Why isn't this script running every 1 minute? Do I need another command to get this cron job running?
You forgot to put user to execute cron job:
*/1 * * * * root /home/coddict/myapp-dev/./spoolemailsender
or
*/1 * * * * root sh /home/coddict/myapp-dev/spoolemailsender
root for example.
Assuming spoolemailsender is executable script and you don't need to do ./spoolemailsender or sh spoolemailsender

Resources