Automatic Overnight Execution of R Scripts on Mac - macos

I want to automate overnight execution of some R scripts in order to have the latest data when I start working. Can you guys tell me how to do that? Via AppleScript or the Automator? or other tools?
Ideally, I would get a sample code looking something like this (AppleScript)
tell R.app
execute example.r
at 2:00 every day
Thx for your help
Andreas

You can use cron combined with Rscript to do this.
First create an R script:
#! /usr/bin/env Rscript
print("Hello World!")
And save it as print_hw.R. Then enter at the terminal:
crontab -e
and choose a time to run the script:
0 0 * * * print_hw.R
This runs the script every night at twelve o'clock. This all is under the assumption that print_hw.R can be executed (use chmod) and the script is in your PATH.

Related

Crontab closing background process?

I am testing a bash script I hope to run as a cron job to scan a download log and perform labor-intensive conversions on image files. In order to run several conversions at once, the first script loops through the download log and sends the filename to the second script, which I set to run as a background process using &.
The script pair works well, but when the process is complete, I must press the enter key to return to a command prompt. This is a non-issue when I am running a test, but I am not sure if this behavior has ramifications when run as a cron job.
Will this be an issue? If so, is there a way to close the "terminal" running the first script from the crontab?
Here's a truncated form of my code:
Script 1 (to be launched by crontab):
for i in file1 file2 file3 etc
do
bash /path/to/convert.sh $i &
done
exit 0
Script 2 (convert.sh)
fileName=${1?no file given}
jpegName=$(echo $fileName | sed s/tif/jpg/g)
convert $fileName $jpegName
exit 0
Thanks for any help/assurances you can give!
you don't need script 2. you can convert it to function and put it inside script1.
Another problem is you are running convert.sh in an uncontrolled way. You cannot foresee how many processes will be created (background processes) and this may lead to severe performance overheads.
finally, if you cannot end process in normal way, you may choose to terminate it again using cron by issueing pkill script1.sh

How to run shell script in background while start?

I have a shell script contain loop like this:
while true
do
if ... ; then
...
else
...
break
fi
done
I want this script to run backend while OS start. I have try to add this script into /etc/rc.d/local.rc as startup script. But OS start too long, and after OS start up, this script did not exist.
So how to add this script into backend while OS start up? And I need this script also could be start up by hand in backend. Thank you~
If you are scheduling on either Linux or Mac, then you can schedule through crontab.
Open terminal.
$crontab -e #use sudo to run as administrator
#add below line
#reboot sh /absolute_path/script.sh
Give the absolute path of the script, save the crontab and exit.
The script will start running from the next reboot.

shell script execute background but can not find

I made shell script
this shell script name is torrent_alert
This code "for" infinity loop execute every 10 second(sleep 10)
So I execute this script background
a few days ago I can find my script on process list use this command
ps ax | grep torrent_alert
But since yesterday I can't find my script is working or not working
I try to reboot, but it doesn't help me
So my question is I want to know my script is alive or dead and process number
How can I solve this problem?
P.S. I use script in Ubuntu

How to automate execution of commands by day and hour on Debian?

So I am a working on a simple home lightning control using Raspberry Pi (and raspbian). I can turn or dim lights by writing commands to a zigbee dongle (through a serial interface) by running a command like:
sudo echo "#1*##*1231#*9#" > /dev/ttyUSB0
One of its main functions is to program "scenes" so you can turn lights on and off at certain hour or day.
So how can I automate a bash command to run lets say monday, tuesday and saturday at 8:55AM every week? Thanks!
Use crontab. Those threads will help you:
how to set cronjob for 2 days?
Crontab Day of the Week syntax
So:
crontab -e # edit crontab file
and then insert
55 8 * * 1,2,6 /usr/local/bin/my_cool_script
In case crontab is not flexible enough for your needs, you can use Ruby Rufus scheduler

Delay in running a command in a shell script

I have written a shell script which includes some commands to collect various Test logs.But I want to put waiting time before a particular command so that it will run after 30 mins after starting the shell script.Anyone having the knowledge regarding it please help me out.
Thanks in advance
You can use the at command to schedule a job to run at any time:
echo "/my_path/my_script" | at now + 30 minutes
If you want a delay inside your script you can sleep to a specific time using this solution.

Resources