Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
Calling Rscript from linux shell script

Can anyone suggest how I might get this working....

I have an R script that takes several minutes to run and writes a few hundred lines of output. I want to write a shell script wrapper around this R script which will launch the R script in the background, pipe its output to a file and start following the bottom of that file. If the user then enters CTRL-C I want that to kill the shell script and tail command but not the R script. Sounds simple right?

I've produced a simplified example below, but I don't understand why this doesn't work. Whenever I kill the shell script the R script is also killed despite apparently running in the background. I've tried nohup, disown etc with no success.

example.R

for(i in 1:1000){
   Sys.sleep(1)
   print(i)
}

wrapper.sh

#!/bin/bash

Rscript example.R > logfile &

tail -f logfile

Thanks in advance!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The following seems to work on my Ubuntu machine:

#!/bin/bash

setsid Rscript example.R > logfile.txt &

tail -f logfile.txt

Here are some of the relevant processes before sending SIGINT to wrapper.sh:

 5361 pts/10   00:00:00 bash
 6994 ?        00:00:02 update-notifier
 8519 pts/4    00:00:00 wrapper.sh
 8520 ?        00:00:00 R
 8521 pts/4    00:00:00 tail

and after Ctrl+C, you can see that R is still running, but wrapper.sh and tail have been killed:

 5361 pts/10   00:00:00 bash
 6994 ?        00:00:02 update-notifier
 8520 ?        00:00:00 R

Although appending your Rscript [...] command with & will send it to the background, it is still part of the same process group, and therefore receives SIGINT as well.


I'm not sure if it was your intention, but since you are calling tail -f, if not interrupted with Ctrl+c, your shell that is running wrapper.sh will continue to hang even after the R process completes. If you want to avoid this, the following should work,

#!/bin/bash

setsid Rscript example.R > logfile.txt &

tail --pid="$!" -f logfile.txt

where "$!" is the process id of the last background process executed (the Rscript [...] call).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!