Edit crontab in jail

Status
Not open for further replies.

LIGISTX

Guru
Joined
Apr 12, 2015
Messages
525
I have having issues trying to edit roots crontab in a jail. I want to run a simple script every ~20 minutes but it can’t seem to get it to kick off.

I did try restarting the cron service in the jail and that didn’t help anything.

Any advice would be very much appreciated.


Sent from my iPhone using Tapatalk
 

DrKK

FreeNAS Generalissimo
Joined
Oct 15, 2013
Messages
3,630
Almost certainly (99.999112% chance) the problem is syntax.

Tell us which crontab file (full path) you put your thing into, and let us see the line you added to the crontab with sufficient context.
 

LIGISTX

Guru
Joined
Apr 12, 2015
Messages
525
I am trying to run a script in my plex jail. I used to have a cronjob from the freenas webui run it, but that no longer works and I have no idea why. I can run the script in the jail, I can ssh into the remote machine from the jail and from freenas itself so the RSA key works in both places, but I wasn't getting any responses to my other thread so I gave up and am just trying to make it work. I don't mind using crontab as its a good learning experience since I am new to all of this anyways.

path is within the jail:

Code:
/etc/crontab


Code:

# /etc/crontab - root's crontab for FreeBSD
#
# $FreeBSD: releng/11.0/etc/crontab 194170 2009-06-14 06:37:19Z brian $
#
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
#
#minute hour	mday	month   wday	who	 command
#
*/5	 *	   *	   *	   *	   root	/usr/libexec/atrun
#
# Save some entropy so that /dev/random can re-seed on boot.
#*/11   *	   *	   *	   *	   operator /usr/libexec/save-entropy
#
# Rotate log files every hour, if necessary.
0	   *	   *	   *	   *	   root	newsyslog
#
# Perform daily/weekly/monthly maintenance.
1	   3	   *	   *	   *	   root	periodic daily
15	  4	   *	   *	   6	   root	periodic weekly
30	  5	   1	   *	   *	   root	periodic monthly
#
# Adjust the time zone if the CMOS clock keeps local time, as opposed to
# UTC time.  See adjkerntz(8) for details.
1,31	0-5	 *	   *	   *	   root	adjkerntz -a
#
# Run Sync1 on 00, 20, 40
*/20	*/1	 *	   *	   *	   root	/root/sync1.sh
#
# Run Sync2 on 00,15,30,45
*/15	*/1	  *	  *	   *	   root	/root/sync2.sh



Ideally, I would like to make them not both run on the hour. But I can't even get this working so I figured I would wait and figure that out next.

I think I could do or something to that affect correct?:
Code:

# Run Sync2 on 15,30,45
15,30,45	*/1	   *	   *	   *	   root	/root/synct2.sh

 
Last edited:

LIGISTX

Guru
Joined
Apr 12, 2015
Messages
525
Almost certainly (99.999112% chance) the problem is syntax.

Tell us which crontab file (full path) you put your thing into, and let us see the line you added to the crontab with sufficient context.

Any advice? Or anyone else for that matter?


Sent from my iPhone using Tapatalk
 

LIGISTX

Guru
Joined
Apr 12, 2015
Messages
525
.... Bump .... ?

Someone has to have an answer for this issue.
 

fracai

Guru
Joined
Aug 22, 2012
Messages
1,212
First, I wouldn't recommend editing the crontab directly. Instead, use the crontab command. If necessary, you can change the EDITOR environment variable.

Beyond that, it looks like your syntax is fine. There's no need to use '*/1'; '*' already means "every". What you have now will run every 15 minutes except the top of the hour. If you want to include that you would need '0,15,30,45' or '*/15'.

I would suggest removing the line from the file. And then run 'crontab -e' as the root user. Add your line again (no need for the user column) and then save and exit the editor.

What errors exactly are you getting? How do you know it's not running, but having problems with the commands in the script? The crontab PATH is very basic. You should usually use complete paths for any commands within the script.

Is the script executable? What's the output when you run it from the plex jail shell as the root user?

If it's still "not running", try changing the execution command to something like: "/path/to/script.sh >/tmp/stdout.txt 2>/tmp/stderr.txt" and see what happens.
 

LIGISTX

Guru
Joined
Apr 12, 2015
Messages
525
First, I wouldn't recommend editing the crontab directly. Instead, use the crontab command. If necessary, you can change the EDITOR environment variable.

Beyond that, it looks like your syntax is fine. There's no need to use '*/1'; '*' already means "every". What you have now will run every 15 minutes except the top of the hour. If you want to include that you would need '0,15,30,45' or '*/15'.

I would suggest removing the line from the file. And then run 'crontab -e' as the root user. Add your line again (no need for the user column) and then save and exit the editor.

What errors exactly are you getting? How do you know it's not running, but having problems with the commands in the script? The crontab PATH is very basic. You should usually use complete paths for any commands within the script.

Is the script executable? What's the output when you run it from the plex jail shell as the root user?

If it's still "not running", try changing the execution command to something like: "/path/to/script.sh >/tmp/stdout.txt 2>/tmp/stderr.txt" and see what happens.

I will try crontab -e, and the script does run if I execute as the root user in the jail. I can easily tell if its working if the lock file is created while its running and that isn't happening. So either the cron job is kicking off and it for some reason can't run the scipt, or the cron job just isn't kicking. I am not sure which is the case.

As far as your comment about the PATH, what would a more complex path me? The script is located in my root dir, thus "/root/sync1.sh" is that path. That is the /root of the jail to clarify.

Thanks for the advice, I will give it a shot. I am new to all this....
 

LIGISTX

Guru
Joined
Apr 12, 2015
Messages
525
Tried crontab -e, all I ended up with is 2 blank .txt's. Thats less than helpful :(

I lied, I got the same error I was getting before trying to run it from outside the jail via jexec, which .... is interesting.

Its like the RSA key just isn't getting passed, even though the script will run if I manually run it. Its like it won't execute unless its a real user trying. It is clearly trying to connect to my remote host, but.... its not being able to authenticate. Trying a few things.

mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
mirror: Login failed: Login incorrect
 
Last edited:

LIGISTX

Guru
Joined
Apr 12, 2015
Messages
525
*/1 * * * * bash -I -c "/root/sync.sh >/tmp/stdout_2.txt 2>/tmp/stderr_2.txt"

that is what I have in crontab -e

Without the bash -c it won't run, and I get the same mirror error with -I or without.
 

fracai

Guru
Joined
Aug 22, 2012
Messages
1,212
so I take it that the script does now run from cron, but the ssh command is failing?

Try increasing the ssh verbosity: 'ssh -vvv'
 

LIGISTX

Guru
Joined
Apr 12, 2015
Messages
525
so I take it that the script does now run from cron, but the ssh command is failing?

Try increasing the ssh verbosity: 'ssh -vvv'

I will try this, I had to reset my freenas box to factory, something went horribly wrong somewhere, probably a little excessive root use.... I will get back to this once I get the rest of my box set up.

Thanks.
 

babaz83

Dabbler
Joined
Nov 10, 2014
Messages
19
Hi guys,
I've tried everything and spent already 2 days on this.
I'm not able to run a script from cron inside a jail.
The script runs just fine with a manual command.

I've used cron -e

Could you please help me?

This is my cron
Code:
SHELL=/bin/bash
PATH=/usr/share:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
*/1 *	 * * *   root  /bin/sh /usr/share/RcloneBzSync.sh > /var/log/cron.txt


This is my script
Code:
#!/bin/bash
if pidof -o %PPID -x “rclone-cron.sh”; then
exit 1
fi
/usr/local/bin/rclone sync -v --bwlimit 9M --min-age 15m --log-file=/var/log/SyncRcloneBz_simonecrypt.log /mnt/oldbks simonecrypt:
/usr/local/bin/rclone sync -v --bwlimit 9M --min-age 15m --log-file=/var/log/SyncRcloneBz_Share2018crypt.log /mnt/NewBks Share2018crypt:
exit


Thank you !
 

LIGISTX

Guru
Joined
Apr 12, 2015
Messages
525
Hi guys,
I've tried everything and spent already 2 days on this.
I'm not able to run a script from cron inside a jail.
The script runs just fine with a manual command.

I've used cron -e

Could you please help me?

This is my cron
Code:
SHELL=/bin/bash
PATH=/usr/share:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
*/1 *	 * * *   root  /bin/sh /usr/share/RcloneBzSync.sh > /var/log/cron.txt


This is my script
Code:
#!/bin/bash
if pidof -o %PPID -x “rclone-cron.sh”; then
exit 1
fi
/usr/local/bin/rclone sync -v --bwlimit 9M --min-age 15m --log-file=/var/log/SyncRcloneBz_simonecrypt.log /mnt/oldbks simonecrypt:
/usr/local/bin/rclone sync -v --bwlimit 9M --min-age 15m --log-file=/var/log/SyncRcloneBz_Share2018crypt.log /mnt/NewBks Share2018crypt:
exit


Thank you !

I never had any luck getting my script working either. I’m going as far as reading up in how to get ESXi working so I can run other flavors of Linux completely separate from FreeNAS to do the automation I am after, and to separate my storage OS from said automation. I want to just let FreeNAS be FreeNAS lol.

But good luck! If you do get it working that would at least provide insight into what I may have been doing wrong.


Sent from my iPhone using Tapatalk
 

anmnz

Patron
Joined
Feb 17, 2018
Messages
286
It must be a way to make it work. Can somebody help ? Thanks
Suggestion:
- get the shell to tell you what it's doing by invoking the script with "-x" (as in /bin/sh -x /path/to/script)
- capture that output and any other error messages by redirecting stderr as well as stdout (e.g. >>/my/log/file 2>&1)
- think about why the script's shebang line says bash but cron is invoking it with sh, are you sure you know what's happening?
- see if the output you're now capturing tells you anything interesting

(EDIT: fix autoincorrect typos)
 
Last edited:

babaz83

Dabbler
Joined
Nov 10, 2014
Messages
19
Code:
SHELL=/bin/bash
PATH=/usr/share:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
*/1 *	 * * *   root  /bin/sh -x /usr/share/RcloneBzSync.sh >> /var/log/cron.txt 2>&1



Is the syntax correct because I don't see any log created.

Thank you.
 

LIGISTX

Guru
Joined
Apr 12, 2015
Messages
525
Code:
SHELL=/bin/bash
PATH=/usr/share:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
*/1 *	 * * *   root  /bin/sh -x /usr/share/RcloneBzSync.sh >> /var/log/cron.txt 2>&1



Is the syntax correct because I don't see any log created.

Thank you.
I don't remember the logging I tried to do when I had this issue, but I don't think I got any logs either. It was as if the cron job from the webUI wasn't even running IIRC. I could be wrong tho, this was months ago and I am very new to "linux", FreeBSD I know, but I am new to all of it.
 

fracai

Guru
Joined
Aug 22, 2012
Messages
1,212
Are you editing the file manually? Or are you using crontab -e?

I suggest starting simply.

test.sh
Code:
#!/bin/sh
date
env


crontab -e
* * * * * /path/to/test.sh 2>&1 >/path/to/test.txt

The above will run every minute.
If that works, try adding your command to "test.sh".
 

anmnz

Patron
Joined
Feb 17, 2018
Messages
286
Are you editing the file manually? Or are you using crontab -e?

I suggest starting simply.

test.sh
Code:
#!/bin/sh
date
env


crontab -e
* * * * * /path/to/test.sh 2>&1 >/path/to/test.txt

The above will run every minute.
If that works, try adding your command to "test.sh".
I'm going to 100% endorse this approach... but with the tiny caveat the the order of operations you want is
Code:
* * * * * /path/to/test.sh >/path/to/test.txt 2>&1

in order to capture stdout and stderr both into the file.
 

babaz83

Dabbler
Joined
Nov 10, 2014
Messages
19
I'm going to 100% endorse this approach... but with the tiny caveat the the order of operations you want is
Code:
* * * * * /path/to/test.sh >/path/to/test.txt 2>&1

in order to capture stdout and stderr both into the file.
BAD news...
noting happens looks like cron doesn't execute anything.
I'm starting to lose hope...
 
Status
Not open for further replies.
Top