bnosac / cronR

A simple R package for managing your cron jobs.
Other
290 stars 38 forks source link

Using www-data as a user #58

Closed OdilKurbanov closed 1 year ago

OdilKurbanov commented 1 year ago

Hi there, Thanks for a great package. I'm currently testing cronR. I'm trying to add a new cron on behalf of 'www-data' user from myuser account in RStudio

library(cronR)
f <- glue('/home/myuser/testfolder/crontesting.R')
cmd <- cron_rscript(f)
# cmd

cron_add(command = cmd, frequency = 'minutely', id = 'test1', user = 'www-data')

# cron_njobs()
# cron_ls()

But cron is added on behalf of myuser:

cron_ls(user='myuser')
## cronR job
## id:   test1
## tags: 
## desc: 
# 0-59 * * * * /usr/lib/R/bin/Rscript '/home/myuser/myfolder/crontesting.R'  >> '/home/myuser/myfolder/crontesting.log' 2>&1

And not for www-data user

cron_ls(user='www-data')
# No cron jobs available for user 'www-data'.

What could be the problem? Thank you!

jwijffels commented 1 year ago

Does user myuser has appropriate rights to see and modify cronjobs of user www-data?

jwijffels commented 1 year ago

Or maybe line https://github.com/bnosac/cronR/blob/master/R/cron_add.R#L253 should add flag -u

OdilKurbanov commented 1 year ago

Does user myuser has appropriate rights to see and modify cronjobs of user www-data?

Thank you for your prompt reply. Yes, myuser is a member of the www-data group.

jwijffels commented 1 year ago

You are right, passing the user seems not implemented for adding and removing. It only works for the user which you use to call the function which is the reason why you saw it as user 'myuser'. The usage of the -u flag in all the system calls needs to pass the user. This needs to be changed in cron_add, cron_rmand cron_clear

OdilKurbanov commented 1 year ago

Thank you for further looking into this. I tested from my www-data user too. I'm able to add a cron job but user=myuser is not changing the owner, cron is added for www-data user.

jwijffels commented 1 year ago

Yes, the package is changing the cron job currently only for the current logged in user

OdilKurbanov commented 1 year ago

It's clear now. Thank you for this cool package. Would be really great if you find time to implement adding/removing cron jobs on behalf of other users of the system.

jwijffels commented 1 year ago

I've made the changes in commit https://github.com/bnosac/cronR/commit/58b0616369dea9b35a793c0ff36344dd351db829 to honour the use of argument user in cron_add and cron_rm. Can you install the development version with remotes::install_github("bnosac/cronR") and let me know how it goes.

OdilKurbanov commented 1 year ago

I installed the dev version and run cron_add command

cron_add(command = cmd, frequency = 'minutely', id = 'test1', user = 'www-data')

and I was asked before adding this cron

Are you sure you want to add the specified cron job: '/usr/lib/R/bin/Rscript '/home/myuser/testfolder/crontesting.R' >> '/home/myuser/testfolder/crontesting.log' 2>&1'? [y/n]

then I checked number of cron jobs by users:

cron_njobs(user='www-data')
# No cron jobs available for user 'www-data'.

cron_njobs(user='myuser')
#There are a total of 1 cronR cron jobs and 0 other cron jobs currently running.

Seems cronR version 0.6.4 is not respecting user='www-data' yet.

Thank you for your effort.

jwijffels commented 1 year ago

Can't reproduce this behaviour. I get the following when ran as another user then www-data. Note the must be privileged to use -u.

> myscript <- system.file("extdata", "helloworld.R", package = "cronR")
> 
> f   <- system.file(package = "cronR", "extdata", "helloworld.R")
> cmd <- cron_rscript(f)
> cron_add(command = cmd, frequency = 'minutely', id = 'abc', description = 'My process 1', user = "www-data")
Are you sure you want to add the specified cron job: '/usr/lib/R/bin/Rscript '/home/jwijffels/R/x86_64-pc-linux-gnu-library/3.6/cronR/extdata/helloworld.R'  >> '/home/jwijffels/R/x86_64-pc-linux-gnu-library/3.6/cronR/extdata/helloworld.log' 2>&1'? [y/n]: 
1: y
Adding cronjob:
---------------

## cronR job
## id:   abc
## tags: 
## desc: My process 1
0-59 * * * * /usr/lib/R/bin/Rscript '/home/jwijffels/R/x86_64-pc-linux-gnu-library/3.6/cronR/extdata/helloworld.R'  >> '/home/jwijffels/R/x86_64-pc-linux-gnu-library/3.6/cronR/extdata/helloworld.log' 2>&1
must be privileged to use -u

As user root If I run this as root. The cronjob is put for user www-data

> library(cronR)
> myscript <- system.file("extdata", "helloworld.R", package = "cronR")
> 
> f   <- system.file(package = "cronR", "extdata", "helloworld.R")
> cmd <- cron_rscript(f)
> cron_add(command = cmd, frequency = 'minutely', id = 'abc', description = 'My process 1', user = "www-data")
Are you sure you want to add the specified cron job: '/usr/lib/R/bin/Rscript '/usr/local/lib/R/site-library/cronR/extdata/helloworld.R'  >> '/usr/local/lib/R/site-library/cronR/extdata/helloworld.log' 2>&1'? [y/n]: 1: y
Adding cronjob:
---------------

## cronR job
## id:   abc
## tags: 
## desc: My process 1
0-59 * * * * /usr/lib/R/bin/Rscript '/usr/local/lib/R/site-library/cronR/extdata/helloworld.R'  >> '/usr/local/lib/R/site-library/cronR/extdata/helloworld.log' 2>&1
> cron_ls(user="www-data")

## cronR job
## id:   abc
## tags: 
## desc: My process 1
0-59 * * * * /usr/lib/R/bin/Rscript '/usr/local/lib/R/site-library/cronR/extdata/helloworld.R'  >> '/usr/local/lib/R/site-library/cronR/extdata/helloworld.log' 2>&1

> cron_njobs(user = "www-data")
There are a total of 1 cronR cron jobs and 0 other cron jobs currently running.
> cron_clear(user = "www-data")
Are you sure you want to clear all cron jobs for 'www-data'? [y/n]: 1: y
Crontab cleared.
> cron_njobs(user = "www-data")
No cron jobs available for user 'www-data'.
OdilKurbanov commented 1 year ago

Let me double-check, I will get back to you with the result. Thanks

OdilKurbanov commented 1 year ago

I'm checking with user in sudo group but not root, and getting must be privileged to use -u warning. Looking how to grant using -u . Will update on progress.