LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Cron Job Help (http://www.linuxquestions.org/questions/programming-9/cron-job-help-679040/)

grande25 10-25-2008 06:39 PM

Cron Job Help
 
Hello everyone,

I'm trying to make a cron job work but I'm having the toughest time with "crontab -e".

When I enter crontab -e it takes me to my vi editor and lists the default cron jobs (hourly, daily, etc). I've tried to go to the last line and add a new * * * * * "file path" formatted line. I've tried editing the existing lines but the result is always the same. No matter what I do in crontab -e it tells me that it's a read-only file system.

How can I overcome this and get some cron jobs working?

Also, if I ever do get a crontab editor working... I feel comfortable with the first part of the cron line format (* * * * *) but not too comfortable with the path info. I can run the script from root as ./filename and it runs just fine so what syntax would go in the path line... just the filename or ./filename??

Thanks for any help with this.

iz

chrism01 10-25-2008 07:12 PM

1. If you want to run a cron job as yourself, non-root, then run crontab -e as yourself (not root). That should solve your permissions problem.
If you want to run it as root, do it from the root acct.
If you've still got the above problem, show us your /etc/fstab file:

ll /etc/fstab
cat /etc/fstab

its possible that partition is mounted read-only

2.: in cron, always use full path to all your progs and any progs/files that they call. The default cron env is minimal.

3. If cron has a problem, it'll normally email root and or the owning user.

jlinkels 10-25-2008 10:53 PM

You can set the editor of choice with the VISUAL environment variable if you are not feeling comfortable with vi.

Code:

export VISUAL=nano
jlinkels

grande25 10-26-2008 09:48 AM

Need more info
 
Chris,

first, thanks for trying to help.

1. How do I run it as my self? As far as I know, I've been running it from the root. That is, after boot up I'm not changing any directories. Are you saying that i need to "cd /usr...something??" Sorry, I'm really not tracking. What do you mean by root acct? I've been searching for hours and all the info I find on this stuff assumes that I already know how to tell when I'm in the "root" or how to be in my "user" tree/directory. How do you get a reference for where you currently are in the tree structure?

2. Okay, from start up and without changing any directories with cd (i'm assuming that's "root") I can run my script with ./scriptname. So, if that's in the root directory, what is the actual syntax in the cron? Is it ./scriptname or /scriptname??

3. I'm not interested in mail. My linux box is strictly a remote client that I'm controlling from a local server. It does nothing more than serve as a bulletin board. I'm just trying to get it set up to do a few things on a timed schedule.

Thanks again,

iz


1. If you want to run a cron job as yourself, non-root, then run crontab -e as yourself (not root). That should solve your permissions problem.
If you want to run it as root, do it from the root acct.
If you've still got the above problem, show us your /etc/fstab file:

ll /etc/fstab
cat /etc/fstab

its possible that partition is mounted read-only

2.: in cron, always use full path to all your progs and any progs/files that they call. The default cron env is minimal.

3. If cron has a problem, it'll normally email root and or the owning user.
__________________
Cheers
Chris

Telemachos 10-26-2008 10:13 AM

Quote:

Originally Posted by grande25 (Post 3322291)
1. How do I run it as my self? As far as I know, I've been running it from the root. That is, after boot up I'm not changing any directories. Are you saying that i need to "cd /usr...something??" Sorry, I'm really not tracking. What do you mean by root acct? I've been searching for hours and all the info I find on this stuff assumes that I already know how to tell when I'm in the "root" or how to be in my "user" tree/directory. How do you get a reference for where you currently are in the tree structure?

Being root has nothing to do with where you are in the directory tree. It has to do with how you log into your system. If you log in as username root and then the root password, you're root. If you log in with a regular user name and then that user's password, you're a regular user. (There are wrinkles later, because the root user can use su to become other users, and some regular users can su to become root, but that's not important right this second.)

Let's start at a more basic level: How are logging into the system, and what crontab do you want to edit? One way to answer the second question is to decide if you will need super-user privileges for any of the things you want the crontab to run.

grande25 10-26-2008 03:36 PM

Quote:

Originally Posted by Telemachos (Post 3322301)
Being root has nothing to do with where you are in the directory tree. It has to do with how you log into your system. If you log in as username root and then the root password, you're root. If you log in with a regular user name and then that user's password, you're a regular user. (There are wrinkles later, because the root user can use su to become other users, and some regular users can su to become root, but that's not important right this second.)

Let's start at a more basic level: How are logging into the system, and what crontab do you want to edit? One way to answer the second question is to decide if you will need super-user privileges for any of the things you want the crontab to run.


I want to edit ANY crontab that is not exclusive to any users. I want it to be a function common to the system and NOT exclusive to any user. I want the cron to run as soon as the Linux client has power.

Without loggin in, I sit at the local Linux client and turn it on. From there, I press ctrl-alt-5 to open a linux command prompt interface. From there, I've been able to create a script that I'm trying to work into a cron that runs as soon as the box comes on... not specific to any user but common to all users.

Also, I don't know if this helps but... The preface to the command prompt cursor when I boot up and open the Linux command prompt interface is: sh-3.2#

grande25 10-26-2008 04:55 PM

Quote:

Originally Posted by jlinkels (Post 3322017)
You can set the editor of choice with the VISUAL environment variable if you are not feeling comfortable with vi.

Code:

export VISUAL=nano
jlinkels


Thanks for that.

chrism01 10-26-2008 06:46 PM

It would help a lot if you told us which distribution & version of Linux you've got.
Its very unusual not to have to login...
However, the prompt ending in '#' usually means you are logged in as root user. This is the equivalent to the admin user (aka 'god') in MS.
You can check by using the cmd

whoami

at the cmd line.

When I mentioned mail, what I meant was that usually an email server is running internally, which various services, inc cron, use to send notices to users, inc root, if an error occurs.
Also, sometimes just for information. It won't normally be configured to send email externally to other systems.
You can check by issuing the cmd

mailx

Or

mail

at the cmd line.

cron is a system service that is start by the OS at boot time and runs forever. If you want a job to be repeated indefinitely at set intervals, that is the right tool.
If you only want a job run once at boot time, let us know, as its a different technique altogether.

To find out what directory you are in, use the cmd

pwd

Do that in the dir your script is in.
Post the contents of your script.

As above we need you to run the cmds

ls -l /etc/fstab
cat /etc/fstab

Post the results of all the cmds I've mentioned, so we can see them.

You really need to bookmark/read this: http://rute.2038bug.com/index.html.gz

grande25 10-26-2008 07:40 PM

sh-3.2# is at command prompt so I assume that is the version?

whoami = root
pwd = /
mail/mailx = command not found... and I don't care about mail.
"cron is a system service that is start by the OS at boot time and runs forever. If you want a job to be repeated indefinitely at set intervals, that is the right tool." Yes, this is what I want.

script I want cron to run:

ping -c 15 123.45.678.910
if [ $? -ne 0 ]
then
echo $?
shutdown -r -t 10 now
fi

ls -l /etc/fstab = -rw-r--r-- 1 root root 3301 2008-10-26 18:07 /etc/fstab

cat /etc/fstab = Well, I'm going to have to wait until tomorrow to get a flash drive so i can transfer over the text file

I couldn't open: http://rute.2038bug.com/index.html.gz

Thanks again,

Iz

Telemachos 10-26-2008 09:03 PM

Quote:

Originally Posted by grande25 (Post 3322617)
sh-3.2# is at command prompt so I assume that is the version?

Nope, the number you see there is the version of your shell (probably Bash). This command might tell you more about your operating system:
Code:

uname -a
Quote:

Originally Posted by grande

It's a webpage. What did you try to open it with?

grande25 10-27-2008 03:40 PM

uname -a = Linux 001cc040e18b 2.6.26-ARCH #1 SMP PREEMPT...

So anyone have some actual help with this?

raconteur 10-27-2008 04:09 PM

Your script:

Code:

ping -c 15 123.45.678.910
if [ $? -ne 0 ]
then
echo $?
shutdown -r -t 10 now
fi

probably won't run the way you expect when invoked by cron.

cron has no stdout/stderr and a minimal environment. The ping command wants to echo to stdout and/or stderr, neither of which are available to cron. In most cases, babble from scripts invoked by cron is sent by mail to the crontab's owner (in this case, root).

Try redirecting stdout and stderr to a file, then reading from that file to determine whether to invoke the shutdown command.

Have you solved the permissions problem with the editor?

Telemachos 10-27-2008 04:13 PM

Quote:

Originally Posted by grande25 (Post 3323337)
uname -a = Linux 001cc040e18b 2.6.26-ARCH #1 SMP PREEMPT...

So anyone have some actual help with this?

It's hard to help since we are guessing at a fairly unusual situation. Normally, the root user should be able to edit whatever files he or she wants. As Chris suggested, the problem may be that your drive is mounted read-only. You still haven't posted the output of cat /etc/fstab which would help.

(Off-topic: I'm having a very hard time understanding your set up: you apparently have a box with Arch Linux on it, the machine allows direct-to-root logins with no password (?!?), and you're running it with a pretty limited sense of how *nix systems work. How exactly did you get into this situation?)

grande25 10-27-2008 04:50 PM

lol, well i'm currently trying to figure out why I can't even get my flash drive to work. I plug it in, do lsusb and can see a flash drive but i can't seem to access it. My fstab doesn't include anything about usb so i think i'm gonna try to load a line into the fstab and see if that helps.. then maybe i can get my fstab over here.

Is there anything else in particular that I should have in my fstab that would let me do stuff with cron?

grande25 10-27-2008 05:21 PM

Woo, i'm getting marginally better at this. Here's the fstab (I just now added the /dev/sda1 /flash vfat defaults 0 0 part to get my flash drive working lol):

# glibc 2.2 and above expects tmpfs to be mounted at /dev/shm for
# POSIX shared memory (shm_open, shm_unlink).
# (tmpfs is a dynamically expandable/shrinkable ramdisk, and will
# use almost no memory if not populated with files)
# Adding the following line to /etc/fstab should take care of this:


none /tmp tmpfs defaults 0 0
none /lib/splash/cache tmpfs defaults 0 0
none /var/log tmpfs defaults 0 0
none /var/spool/cups tmpfs defaults 0 0
none /usr/share/X11/xkb/compiled tmpfs defaults 0 0
none /var/run tmpfs defaults 0 0
none /var/tmp tmpfs defaults 0 0
none /var/lib/dhcpcd tmpfs defaults 0 0
none /root tmpfs defaults 0 0

proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
none /opt/wine-root/drive_c/windows/temp tmpfs defaults 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/sda1 /flash vfat defaults 0 0
/dev/hda1 / ext3 defaults,noatime 0 0
/dev/hda1 / ext3 defaults,noatime 0 0


All times are GMT -5. The time now is 09:52 AM.