How do I add a cronjob to a makefile? - unix

How do I add a cronjob to a makefile so when I compile a C program, I add a cron job to crontab so that program gets executed every minute:
Cron job I am trying to add:
* * * * * /Users/jenna/desktop/ && ./myProgram command_line_arg1
how do I add this to a make file ?

I believe it is a bad idea to add a crontab job from a Makefile. Adding a cron job is a sysadmin issue. Compiling your program is for a developer's hat.
If you insist on doing that, you might use crontab -l > mycrontab to list the current crontab jobs, then appending with
echo '* * * * * cd /Users/jenna/desktop/ && ./myProgram command_line_arg1' \
>> mycrontab
you probably want to make that more robust (perhaps use grep or awk) to avoid the appending if the crontab already contains that. BTW, you probably want to use $HOME/Desktop (and even that might not work on a French configured user, it would be $HOME/Bureau).
Then, set the new crontab with crontab mycrontab
At last, running something every minute seems suspicious to me. What is your program doing?
I would suggest to at the very least prompt the user before updating his/her crontab file....
Perhaps you want the at command....

Related

Crontab Redhat 8: Crontab will not run an R script that works perfectly otherwise

I'm currently trying to run a R file every minute through crontab. The R file take in a file from the previous directory and inputs into a sub-directory. If I just ./script.R the command will run fine, read and output all the data 100% accurately. However, in crontab it will not even run. I have tried a sample crontab to test if it was my crontab not working first, I wrote
* * * * * /bin/echo "foobar" >> /home/a/t/test.log
This command would correctly send foobar to the log.
However when I did
* * * * * /home/a/t/script.R >> /home/a/t/test.log
I would get no results within the test.log, nor would I get results in the subdirectory it should store in after being ran. I also did the script another way to see if the * were causing issues
*/1 * * * * /home/a/t/script.R >> /home/a/t/test.log
This also did not give any results.
After some research what I can understand is that there is likely an issue with paths. So to try to counteract that I did a cd command along with the R command as such:
*/1 * * * * cd /home/a/t/ && /home/a/t/script.R >> /home/a/t/test.log
Still, however, this has given 0 output. If I type
grep CRON /var/log/cron
I see cron trying to run the lines every minute, but again no output found anywhere, I dont think it is running and I can't understand why not.
Also in terms of path for the R script itself, within the R script I have the first line written something like
#!/bin/env Rscript -S --vanilla
which allows me to ./script.R and run it after a chmod as well.
This exact crontab commands also worked for me on my other laptops ubtunu vm, which is causing more confusion.
The command for running Rscript should be
Rscript path/to/Rscript.R
Please test the command by running it in your terminal. Once it run perfectly fine then just take the whole command and put it in crontab.

How to run cron job automatically without any action in wordpress?

I have used wp_schedule_event but this is not full my requirements.It can be triggered on any click or reload the page.
How can i create cron job that automatically trigger on a specific time without any action in wordpress.
The only way, as far as I know is to use the cron ability of your server.
For example, in Red Hat based distributions such as CentOS, crontab files are stored in the /var/spool/cron directory, while on Debian and Ubuntu files are stored in the /var/spool/cron/crontabs directory. Open crontab file and write something like:
* * * * * php /var/www/html/your-website/your-cron.php
This will execute the file your-cron.php every minute.
Note: Although you can edit the user crontab files manually, it is recommended to use the crontab command.

Schedule a Rscript crontab everyminute

For some reason my R script will not run with a crontab. I have it for every minute right now for testing, but will change it once it works.
Any ideas?
* * * * * Rscript “/Users/Home/Desktop/David Studios/Scraper/compiler.R”
Also, this was working as just a normal command in Terminal.
I can see the dreaded smart quotes in your cron entry. This often happens when you copy-paste from word processors. Backspace over those abominations and re-type normal quotes. Change:
* * * * * Rscript “/Users/Home/Desktop/David Studios/Scraper/compiler.R”
to
* * * * * Rscript "/Users/Home/Desktop/David Studios/Scraper/compiler.R"
See the difference? It's subtle and easy to miss.
Update:
I see you've made the above change and it's still not working for you. Verify that Rscript is in the $PATH environment variable for the user that owns this crontab. Alternatively, you can simply specify the fully qualified path to Rscript directly in the cron entry. You can find that quickly on the command line with the following command:
which Rscript
Update #2:
I see by your comments that the fully qualified path to Rscript is /usr/local/bin/Rscript. I'm guessing /usr/local/bin is not in the path for the user who owns this crontab. Try using the fully qualified path, like this:
* * * * * /usr/local/bin/Rscript "/Users/Home/Desktop/David Studios/Scraper/compiler.R"
Check that you are really running crontab deamon. You should get a number as return, which is the process id for crontab.
pgrep cron
Make sure your R file is execuable:
sudo chmod +x [yourfile.R]
Add the shebang line in your R file:
#!/usr/local/bin/Rscript
Let crontab do the change of directory:
* * * * * cd /Users/Home/Desktop/David Studios/Scraper/ && /usr/local/bin/Rscript compiler.R
You might have a problem with the working directories in R.
When you run the script from the terminal you might be in the directory where the files the script needs are, but when the script runs with cron it uses another directory.
Use the setwd() function inside the R script or use absolute paths when accesing files to make sure the script works no matter where it is being used.
I just spent several hours on a similar problem. I'm still not completely sure what was wrong, but the following command finally worked and I don't really care why. Hopefully this can help someone else.
* * * * * /usr/local/bin/Rscript -e 'source("/home/rstudio/git/myproject/inst/please_work.R")'

scheduling meteor.js on crontab

I have a meteor.js application I've created to run locally on OSX on my laptop. Every so often, meteor stops running for unexplained reasons (like after the computer returns from sleep).
I'd like to schedule a process on cron to check every minute if meteor is working, and if it's not, launch it. I placed the following in my crontab:
* * * * * ps aux | grep meteor | grep -v grep || cd ~/path_to_my_meteor_project/; nohup meteor &
This command works at launching meteor when I enter it manually in terminal. But when scheduled in cron, it does not seem to do anything.
This is likely to be caused by the fact cron expects a binary file (or a script to launch) whereas, when typed in a terminal, your command is interpreted by bash, which understands logic such as ||.
In order to fix this, you will have to create a bash script containing the line of code working in your terminal, then ask cron to run it every minute.

Issues with cron

I'm really stuck. I've used cron on many machines, but I simply can not get it to work on an Ubuntu server. I've tried everything I can think over the weekend and I'm baffled. I'm hoping someone can help me out.
I can verify that cron is running via pgrep cron. However, pgrep crond returns nothing. I am trying to run a simple shell script (test.sh) with cron:
#!/bin/sh
/usr/bin/touch /home/jarvis/test.txt
I have run chmod +x on the shell script and my crontab looks like this:
01 * * * * /home/jarvis/test.sh
I also have a new line ending after the line.
Try redirecting all output to a file and see if there are any error messages that can help you diagnose the problme, i.e.
01 * * * * /home/jarvis/test.sh > /tmp/jarvis_test.log 2>&1
Also, if you created any of those files from a windows environment, don't forget dos2unix filename
edit
My point is that you won't know for what your script is doing in the crontab environment unless you see if it is outputing an error message. Error messages also go to local crontab user's email, so try mail (as the same user) and see if you have a bunch of messages from your crontab.
pgrep crond returning nothing sounds like the problem, but I'm not a sysadmin. Maybe you want to flag this and ask for moderator to move to https://serverfault.com/
I hope this helps.
I think it is related to the user permissions. Try putting in default crontab hourly,weekly files, if it works there then your cron is good. Also, check /var/log/messages for any cron entry. View /etc/crontab for detailed configuration.

Resources