Submitting R jobs via single ssh command using nohup bash -c - r

I used to work with an older university server where I could submit analysis jobs using commands like the following. The server was a bit dated and using C Shell.
ssh -t user#oldserver 'cd ~/hte_paper/Code; nohup tcsh -c "R CMD BATCH analysis-file1.r; R CMD BATCH analysis-file2.r" &'
With the Ubuntu 20.04 server images provided by the Uni I work at now, this doesn't work anymore. Neither of the following commands work, nor do any of the variations I've tried. What am I missing? I've been using these commands in Makefiles to automate some work and prefer that over using tmux.
ssh -t user#myserver 'cd ~/some/folder; nohup bash -c "R CMD BATCH analysis-file.r" &'
ssh -t user#myserver 'nohup bash -c "cd ~/some/folder; R CMD BATCH analysis-file.r" &'

A friend helped me figure it out. The following works.
ssh -t user#myserver "set -m; cd ~/some/folder; (R CMD BATCH analysis-file1.r; R CMD BATCH analysis-file2.r) &"

Related

System command fails in R Studio but works in R from Terminal

I have a system command in R studio which fails whereas the same command works from R using Terminal in Mac. Could someone hint what is wrong here:
From RStudio:
> system('/bin/qt query -i /var/folders/z0/kms9x7hd6hgdtbtk3kxnjcjxw2_l57/T//RtmprObPeS/9c810678b567d9a52dec9a86/5.gz -v -d /var/folders/z0/kms9x7hd6hgdtbtk3kxnjcjxw2_l57/T//RtmprObPeS/94047c0ddd9fb36a892047be/0.phe.db -p "Phe=2" -g "count(UNKNOWN)<=0" -p "Phe=2" -g "HOM_ALT" > /var/folders/z0/kms9x7hd6hgdtbtk3kxnjcjxw2_l57/T//RtmprObPeS/9c810678b567d9a52dec9a86/out.vcf')
gqt: SQL error 'no such table: ped' in query 'SELECT BCF_ID FROM ped WHERE Phe=2 ORDER BY BCF_ID;': No such file or directory
The error message appears to have something to do with the tool but it also shows No such file or directory error whereas the same command works from terminal as shown below.
From R Terminal:
> system('/bin/qt query -i /var/folders/z0/kms9x7hd6hgdtbtk3kxnjcjxw2_l57/T//RtmprObPeS/9c810678b567d9a52dec9a86/5.gz -v -d /var/folders/z0/kms9x7hd6hgdtbtk3kxnjcjxw2_l57/T//RtmprObPeS/94047c0ddd9fb36a892047be/0.phe.db -p "Phe=2" -g "count(UNKNOWN)<=0" -p "Phe=2" -g "HOM_ALT" > /var/folders/z0/kms9x7hd6hgdtbtk3kxnjcjxw2_l57/T//RtmprObPeS/9c810678b567d9a52dec9a86/out.vcf')
The terminal command writes the output to the out.vcf file.

sending data to telegraf from windows git bash

How exactly can I send data to telegraf from windows command prompt / git bash?
I'm trying to send data in unix bash to telegraf.
In bash I do:
echo -e "my_db,owner=me,field=value" | nc -u4 -w1 my.telefra.host.com 1231;
You can use telegraf exec input plugin.
It allows you to execute any sort of command, including powershell, cmd, git bash etc. For instance, if I want to run powershell command,
commands = ["powershell -command <command>"]
Same can be done for git bash,
commands = ["<path>\bash.exe <command>"]

Running Docker with infile bash syntax

I need to run R in a Docker container and want to input a script from a volume I mounted using the standard infile notation, however, the file seems to be redirected to Docker, not R.
I'm using the following command:
docker run -v /root/share:/share r-base:latest R --vanilla --quiet < /share/test.r
How can I use the infile notation and run my R in Docker? (I need the direct output from R, so Rscript will not do.)
As #mazel-tov mentioned: try
docker run -v /root/share:/share r-base:latest /bin/bash -c 'R --vanilla --quiet < /share/test.r'
That way the redirection of stdin is done by the bash inside the docker instead of in your shell that starts the docker.

Tmux: How do I find out the currently running version of tmux?

I know that I can run tmux -V to find the version of tmux that is in my PATH, but how can I get the version of tmux that is currently running?
As pointed out in a comment, tmux -V returns the version:
$ tmux -V
tmux 3.0a
Tested on Centos 7 and OSX 12.5.
Most obvious, but not 100% correct way is to execute this command in console
$ tmux -V
and receive output like this tmux 2.9a with version of tmux INSTALLED, not currently running.
In 99% cases it is enough, but there can be subtle nuances.
Command tmux -V will return version of tmux installed at /usr/bin/tmux or any other directory inside your PATH variable. If you have tmux already running, it is possible that tmux can be started from binary of other version and from different place (for example, tmux can be started from /home/user/bin/tmux).
In this case, you have to call
$ ps -e | grep tmux
to see PID of all tmux processes currently running. It will output something like this
[vodolaz095#ivory ~]$ ps -e | grep tmux
19699 pts/0 00:00:00 tmux: client
19701 ? 00:00:00 tmux: server
Here, number 19701 depicts process id (PID) of currently running tmux server.
After getting PID of tmux server, you can ran command
$ lsof -p 19701
to get information about CURRENTLY RUNNING tmux server process (in my case its 19701) that will output something like this (Figure 1)
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
tmux:\x20 19701 vodolaz095 cwd DIR 8,33 4096 22544385 /home/vodolaz095
tmux:\x20 19701 vodolaz095 rtd DIR 8,1 4096 2 /
tmux:\x20 19701 vodolaz095 txt REG 8,1 677760 3675332 /usr/bin/tmux
tmux:\x20 19701 vodolaz095 mem REG 8,1 6406312 131327 /var/lib/sss/mc/group
as you can see, tmux currently running was executed from binary placed in /usr/bin/tmux.
Or, you can call one liner
lsof -p `pgrep 'tmux: server'`
to achieve the same output as Figure 1
After you get path to tmux binary CURRENTLY RUNNING, (in my case, it was /usr/bin/tmux), you can execute this binary with flag -V to get its version
/usr/bin/tmux -V
or, if tmux was installed by limited user into /home/user/bin/tmux,
/home/user/bin/tmux -V
And, as result, you'll get version of tmux currently running, not the one, that was installed.
To get the version of the tmux server you can use display-message.
./tmux2.3 display-message -p "#{version}"
Will show the version of the server (2.7 in my case)
-p will direct the output of stdout so you can script with it and {version} can be anything from the FORMATS section in the man page.
The following will give you the executable of your tmux server, on linux:
realpath /proc/$(tmux display-message -p "#{pid}")/exe
And on macos, proc_pidpath can be used, see https://stackoverflow.com/a/8149380
To find the actual version of the tmux that is running, you have to find the PID of the tmux:
pgrep tmux
With this info, you can check the version by running:
lsof -p $tmuxPID | grep REG | grep -i -e deleted -e "tmux$"
If there is not a (deleted) next to the tmux file listed, you can just run that file with a -V.
If it results in files that are "(deleted)", you are running an old, uninstalled version. If you are on linux, you can figure out what it is by running:
/proc/$tmuxPID/exe -V`
If you are on OS X, you are stuck with whatever information is in the path to the filename, possibly something like Cellar/tmux/<version number>/bin/tmux.
You can combine many of these steps into the following one-liner:
for tmuxPID in $(pgrep tmux); do lsof -p $tmuxPID | grep REG | grep -i -e deleted -e "tmux$"; done
Or if you are on Linux, this always works:
for tmuxPID in $(pgrep tmux); do /proc/$tmuxPID/exe -V; done

How to run multiple R scripts simultaneously?

I would like to run all the R script (script1.R, script2.R, ...) stored in a directory (~/Sims). Moreover I would like that each script run in a separate terminal. The os I'm using is OS X 10.9.5.
I used a bash script with the following commands:
#!/bin/bash
FILES=~/Sims/*.R
for f in $FILES
do
xterm -e bash -c "R --vanilla < $f; exec bash" &
done
I would like to find an alternative for xterm (given that under os x require to install the package X11 and on some machine I can't install it) that it is part of os x (like the Terminal app)
I would like to not exit from the R environment at the end of the R script
This will mimic your xterm configuration but use new Terminal.app sessions instead:
for f in *.R
do
osascript -e "tell app \"Terminal\" to do script \"R --vanilla < /FULL/PATH/TO/${f}\""
done
As far as keeping the R session alive, I'm not sure that's possible.

Resources