1

I added a cronjob via crontab -e which is to be executed every minute. I enabled and started cronie on systemctl.

Set editor

$ export EDITOR=vim

Add cront tasks

$ crontab -e

* * * * * Rscript /srv/shiny-system/cron/CPU.R
~
~
"/tmp/crontab.8VZ7vq" 1 line, 47 characters 

Install cronie

$ sudo pacman -Syu cronie
$ sudo systemctl enable --now cronie.service
$ systemctl status cronie

List cron tasks

$ crontab -l
* * * * * Rscript /srv/shiny-system/cron/CPU.R

I wrote this program below in R, when I start it in the shell it workes fine, but when i start it with cronie it just doesn't do what it is supposed to do.

setwd("/srv/shiny-system/cron")
I <- 0
for (i in 1:60) {
    system("top -n 1 -b -u shiny > top.log")
    dat <- readLines("top.log")
    id <- grep("R$", dat)

    Names <- strsplit(gsub("^ +|%|\\+", "", dat[7]), " +")[[1]]

    if (length(id) > 0) {
        # ‘top’ data frame;
        L <- strsplit(gsub("^ *", "", dat[id]), " +")
        dat <- data.frame(matrix(unlist(L), ncol = 12, byrow = T))
        names(dat) <- Names
        dat <- data.frame(Time = Sys.time(), dat[, -ncol(dat)], usr = NA, app = NA)
        dat$CPU <-as.numeric(as.character(dat$CPU))
        dat$MEM <-as.numeric(as.character(dat$MEM))

        # Check if connection number changed;
        for (i in 1:length(dat$PID)) {
            PID <- dat$PID[i]
            system(paste("sudo -i netstat -p | grep ", PID, " > netstat.log"))
            # system(paste("sudo -i netstat -p | grep ", PID, " >> netstat.log2"))
            system(paste("sudo -i lsof -p ", PID, " | grep DIR > lsof.log"))
            netstat <- readLines("netstat.log")
            lsof <- readLines("lsof.log")
            dat$usr[i] <- length(grep("ESTABLISHED", netstat) & grep("tcp", netstat))

            dat$app[i] <- regmatches(lsof, regexec("srv/shiny-server/(.*)", lsof))[[1]][2]

        }
        dat <- dat[, c("app", "usr")]
    } else {
        dat <- data.frame(app = "app1", usr = 0)
    }
    write.table(dat, file = "CPU.txt")
}

This program generates these 4 files below and cronie seems to be running bcos I can see them in this location /srv/shiny-system/cron,

top.log
netstat.log
lsof.log
CPU.txt

But the problem is there is no log/ data/ info in netstat.log and lsof.log

So is this a problem in my R program or in cronie actually?

But when I run this program with the cron in Ubuntu Linux, I get the data I am after in netstat.log and lsof.log.

So is this a bug/ problem in Arch Linux?? It seems that Arch has a problem with netstat and lsof. I have netstat installed as well,

Install net-tools for netstat

$ sudo pacman -S net-tools 

Any ideas?

  • Does the top.log contain the right data? have ou tried deleting it and then seeing if it is created again? I just wan to make 100% sure that the crontab is actually being run and is correctly executing the R script. By the way, kudos for using R of all things for something like that. You could simplify it enormously to just a few lines of bash; ask a question explaining what you are logging if you're interested. – terdon Feb 22 '16 at 13:09
  • Does the top.log contain the right data? Yes it does. – laukok Feb 22 '16 at 13:14
  • 1
    probably, yes. The command won't run without a password. Try adding it to root's crontab instead and removing the sudo.. – terdon Feb 22 '16 at 13:30
  • 1
    no, crontab -e will add it to whichever user's is running the command. You can get root's with sudo crontab - e. – terdon Feb 22 '16 at 14:44
  • 1
    Let us continue this discussion in chat. – terdon Feb 22 '16 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.