LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-28-2022, 11:26 AM   #1
1s440
Member
 
Registered: Mar 2018
Posts: 266

Rep: Reputation: Disabled
cronjob as a user


Hi all,

I wanted to copy the file from one server to other server, i used scp to copy it. it works

Code:
saggu@myserver: scp saggu@test:/tmp/tests.csv /opt/storage/
Now i wated to schedule cronjob for the same. But the cronjob always try to run as root user, how to make cronjob to run as 'saggu' user and after the cronjob success i wanted to change the owner ships of the csv file. how to do it in a best way

I tried this but doesnot work
Code:
/4 *   * * *   saggu 'scp saggu@test:/tmp/tests.csv /opt/storage'

Last edited by 1s440; 11-28-2022 at 11:29 AM.
 
Old 11-28-2022, 11:38 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,739

Rep: Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921
How are you adding the cron job?

Running crontab -e as your regular user should run that command as that user.

Code:
/4 *   * * *  scp saggu@test:/tmp/tests.csv /opt/storage
I assume you are using ssh keys?
 
Old 11-28-2022, 11:56 AM   #3
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
How are you adding the cron job?

Running crontab -e as your regular user should run that command as that user.

Code:
/4 *   * * *  scp saggu@test:/tmp/tests.csv /opt/storage
I assume you are using ssh keys?
i have added the cronjob under /etc/crontab, i used it as sudo /etc/crontab to edit crontab. I am not able to use crontab as saggu user, it allows me to edit and save , i also see 'installing as new crontab' but nothing changes. when i look again using 'crontab -l' i see no crontab for saggu
I used ssh keys

Last edited by 1s440; 11-28-2022 at 12:07 PM.
 
Old 11-28-2022, 12:12 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,739

Rep: Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921
Simplest would be to switch to your user's cron job instead of system cron.
 
Old 11-28-2022, 12:26 PM   #5
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,671

Rep: Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711
FYI: user cron jobs are normally maintained using the crontab command, rather than manually manipulating files. Crontab has some syntax and issue checking, but it is always better to not depend upon that.
 
Old 11-28-2022, 12:30 PM   #6
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Simplest would be to switch to your user's cron job instead of system cron.
yes it works with the user crontab, but the problem is after scp i would need to modify the file permissions and ownerships which is only possible with sudo user then
 
Old 11-28-2022, 12:32 PM   #7
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,671

Rep: Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711
Quote:
Originally Posted by 1s440 View Post
yes it works with the user crontab, but the problem is after scp i would need to modify the file permissions and ownerships which is only possible with sudo user then
Why?
The permissions can be set before transfer, and the ownership depends upon the account on the remote host.
 
Old 11-28-2022, 01:29 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,739

Rep: Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921Reputation: 5921
If you want to change the owner of the file then you need to run the cron job as root. Create a script that includes scp and your chown command and then run it as root in /etc/crontab.

If I run a cronjob in /etc/crontab as my user i.e.
* * * * * user scp ...

or in my users crontab the permissions are the same i.e.

-rw-r--r-- 1 user user .... my_file

Last edited by michaelk; 11-28-2022 at 01:40 PM.
 
Old 11-28-2022, 10:34 PM   #9
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,671

Rep: Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711Reputation: 2711
My scp man page mentions the -p option, which preserves the owner and group, and permissions. If those are set properly for the target location and ownership (may have to set numerically) and you scp it using the -p option then it should land with the proper ownership and permission. (Under some conditions the advantage may be sending TO the root account on the remote machine, so the authority to force that is in place, but I cannot test on your hosts to make sure if that might be required for your case.)

My OpenSSH version (source of the scp command) is 9.1p1.


I hope that helps.

Note that this ownership and permission issue is an enhancement or expansion of the original question. You might work on including ALL of the requirements in the original post for future questions.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cronjob: Could not authenticate PAM user ... Authentication failure (for root?!?) JZL240I-U Linux - Security 11 03-14-2019 01:05 PM
How to make a cronjob or at job user interactive kiranb12 Programming 4 09-03-2010 04:56 AM
User cronjob via crontab is hanging JockVSJock Programming 8 06-18-2009 09:41 PM
How to set up a cronjob as a normal user ? gauthamk Linux - Software 1 09-06-2008 10:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration