Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Crontab


  • Please log in to reply
14 replies to this topic

#1 KJackson50

KJackson50

  • Members
  • 95 posts
  • OFFLINE
  •  
  • Local time:10:12 PM

Posted 16 December 2014 - 10:06 PM

Hey guys,

 

I'm having some issues with crontab. I have added a couple lines to crontab, however I don't think they are running. When I look at the logs it shows that it ran, but I can tell my script didn't run since I didn't get an e-mail.

 

When I put a script into the cron.daily or cron.hourly directories they run fine. I'm running Centos7. Let me know what you guys need to see and I'll supply.

 

Thanks in advance.



BC AdBot (Login to Remove)

 


#2 wizardfromoz

wizardfromoz

  • Banned
  • 2,799 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:01:12 PM

Posted 17 December 2014 - 02:45 AM

Hi KJackson50 - I know next to nothing about crontab, but the cavalry should be along soon, and cat1092 used to run a version of CentOS as well.

 

Have you posted screenshots here before? You will need an image hosting site (free), see Moderator Stolen's post at this Topic.

 

I use Imgur, cat 1092 uses Photobucket, NickAu uses TinyPic - each has its merits.

 

Also, you could flesh out your specs while you are waiting - see my Profile and my signature below for an example of what is useful to us.

 

Good Luck and I'll swing by to seehow you're doing.

 

:wizardball: Wizard



#3 mremski

mremski

  • Members
  • 493 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:NH
  • Local time:11:12 PM

Posted 17 December 2014 - 12:49 PM

These are entries to be run by root?  Is there anything in the script that gates the sending of the email, or is the email sent by whomever runs the script (run-parts)?  I think on CentOS that the /etc/crontab is mostly a sample and the system cron or root cron is governed by what's in /etc/cron.d.  So if these are things you want done by root, you can put them in cron.daily/hourly/monthly/whateverly, just remember that they are a local modification if you update the system.  A quick "man cron" CentOS 6.4 system talks about cron searching /etc/anacrontab and /etc/cron.d


FreeBSD since 3.3, only time I touch Windows is to fix my wife's computer


#4 mremski

mremski

  • Members
  • 493 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:NH
  • Local time:11:12 PM

Posted 17 December 2014 - 01:04 PM

Did you google Centos 7 crontab?  There is a topic in centos forums that sounds very much like yours.

There is another log file to look in /var/log/cron, then a bit more that indicating cron is picky about the permissions on /etc/crontab (chmod 644 /etc/crontab seemed to fix it according to the centos forum topic)


FreeBSD since 3.3, only time I touch Windows is to fix my wife's computer


#5 KJackson50

KJackson50
  • Topic Starter

  • Members
  • 95 posts
  • OFFLINE
  •  
  • Local time:10:12 PM

Posted 17 December 2014 - 03:26 PM

Hey sorry for the late responses, I work during the day.

 

I'm running Centos 7 on a digital ocean box. I ssh into the box, or use the console access on the website. I'm familiar with the cron.daily, cron.hourly, etc. I currently have a few scripts running that way. However I wanted to practice and use crontab -e, for situations where I need something done at a specific time. I'm not too sure how to edit the time that cron.daily/hourly/weekly runs. I believe the script in question is run by the account I use (I disabled root access due to security reasons).

 

I'll look at that forum topic, I've been searching for answers for a while now and don't think I've come across that forum post.


Edited by KJackson50, 17 December 2014 - 03:28 PM.


#6 KJackson50

KJackson50
  • Topic Starter

  • Members
  • 95 posts
  • OFFLINE
  •  
  • Local time:10:12 PM

Posted 17 December 2014 - 03:38 PM

Okay, great info in that post. I believe I'm doing something wrong. It seems the poster put his jobs directly in the "sample" crontab file and did a chmod 644 for the permissions.

 

What I do is a simple crontab -e, then edit as normal. I notice when I do crontab -e it saves in this file /tmp/crontab.1J1N8. Guessing that's super wrong!

 

I'm going to move it to the original /etc/crontab file, do a chmod 644 and see what happens. Below is a capture of what I see when I do crontab -e.

 

PqCi1tS.png


Edited by KJackson50, 17 December 2014 - 04:12 PM.


#7 KJackson50

KJackson50
  • Topic Starter

  • Members
  • 95 posts
  • OFFLINE
  •  
  • Local time:10:12 PM

Posted 17 December 2014 - 04:34 PM

Okay guys figured it out with much help from mremski. I went ahead and moved my crontabs to /etc/crontab and did a chmod 644. I tested it out and my jobs ran on time! Or so I thought. My yum_daily is supposed to look for and download updates, then send a tail /var/log/cron to my e-mail. I noticed on the few times I tested it would e-mail me nothing. So not sure if the script ran or not. However when I execute the script form command line it runs and root sends me an e-mail with the tail /var/log/cron.

 

My Crontab

UJiaHE5.png

 

 

Script I'm Testing:

Qofc1T4.png



#8 mremski

mremski

  • Members
  • 493 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:NH
  • Local time:11:12 PM

Posted 18 December 2014 - 06:58 AM

Some of us work all day too, just have moments where the computer is doing something for us :)

 

I believe that yum update needs to run as root;  some of the files it accesses (/var/cache/yum among others) are likely owned by root, probably writeable only by root.  Your crontab entries are running as user KJackson, right?  First one you do a sudo, so that works.  The second, if you tweak the script and do a "sudo /home/KJackson/Scripts/yum_daily" it will likely work.  

The script tweaks:

first line of the file should read:

#!/bin/bash

 

Then after save and exit, do

chmod a+x /home/KJackson/Scripts/yum_daily

 

(a+x means add execute priv to ugo user:group:others.  Other OSs refer to them as owner:group:world)  You can poke around at man chmod if you want to figure out the correct octal value.

 

Your crontab -e:  the editor may create the file in /tmp, but the final result should be in /var/spool/cron (root can look at the directory).  There are some caveats with it (man crontab, look for stuff about cron.allow, cron.deny, PAM)


FreeBSD since 3.3, only time I touch Windows is to fix my wife's computer


#9 KJackson50

KJackson50
  • Topic Starter

  • Members
  • 95 posts
  • OFFLINE
  •  
  • Local time:10:12 PM

Posted 18 December 2014 - 03:40 PM

Good news, the script is working!

 

I did the tweaks prescribed by mremski. The only thing I did different is run it as root and not KJackson. I tried it as KJackson, it was logged as run, but I didn't get an e-mail. What I took from this is making sure my scripts are structured correctly, paying attention to the chmod privileges, and trying to run a crontab as root if the user isn't working.

 

Thanks for all your help mremski.



#10 mremski

mremski

  • Members
  • 493 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:NH
  • Local time:11:12 PM

Posted 19 December 2014 - 04:41 AM

No problem.  A couple other things to take away:

Professor Google is your friend.  Even doing a search for man crontab in a browser and following links is helpful.

Unix/Linux/BSDs:  there are often many ways to do things, every expert has their way, listen to them all.

"grep, awk, sed, regular expressions";  get used to them, astound your friends.

 

Unless you're trying to start a war, don't ask "Which is better, Emacs or Vi?"


FreeBSD since 3.3, only time I touch Windows is to fix my wife's computer


#11 mremski

mremski

  • Members
  • 493 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:NH
  • Local time:11:12 PM

Posted 19 December 2014 - 04:44 AM

Forgot to add:

Did you try, as user KJackson (logged in as, not su'd to from root) doing:

 

sudo yum_daily?

 

That's basically what your crontab entry was trying to do, it may help debug why it didn't work.  I'm guessing permissions on something yum/rpm related.


FreeBSD since 3.3, only time I touch Windows is to fix my wife's computer


#12 KJackson50

KJackson50
  • Topic Starter

  • Members
  • 95 posts
  • OFFLINE
  •  
  • Local time:10:12 PM

Posted 20 December 2014 - 12:13 AM

When I tried as user KJackson it didn't work, but when I did it as user root it worked.



#13 bmike1

bmike1

  • Members
  • 596 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Gainesville, Florida, USA
  • Local time:10:12 PM

Posted 20 December 2014 - 02:41 AM

permissions are wrong on the file then.

 

  open terminal

  sudo updatedb

  locate <cronfile name>

  sudo chmod 700 </path/to/cronfile>

 

That should fix it. What you are doing is giving your user permission to read, write, and execute (7) on the cronfile while giving no permissions to group (0) and others (0). If you want to learn about permissions do 'man chmod' To sum it up (look at 700) user is the first place (the 7), group is the second, and others is the third. Then the permissions you can grant and their (values) are: read (4) write (2) and execute (1).You then add the values you want to determine the number you want to give the user, group, and others. So say you wanted to read and execute permission? The octal is 5 (4+1). You want execute and write? The octal is 3. You only want execute? It is 1 in that case. And if you don't want any permissions (only root can access) the octal is 0.

 

if that doesn't fix it the problem might be that your user doesn't own the file. You can verify this  with

 

  ls -l (those are lower case 'L' <list><long>)

 

Your user should appear in the 3rd  field. The third and fourth fields are the file's owner and group. Or just run the command:

 

  sudo chown <user> <file>  

 

that changes ownership to user for the specified file.


Edited by bmike1, 20 December 2014 - 03:10 PM.

A/V Software? I don't need A/V software. I've run Linux since '98 w/o A/V software and have never had a virus. I never even had a firewall until '01 when I began to get routers with firewalls pre installed. With Linux if a vulnerability is detected a fix is quickly found and then upon your next update the vulnerability is patched.  If you must worry about viruses  on a Linux system only worry about them in the sense that you can infect a windows user. I recommend Linux Mint or, if you need a lighter weight operating system that fits on a cd, MX14 or AntiX.


#14 bmike1

bmike1

  • Members
  • 596 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Gainesville, Florida, USA
  • Local time:10:12 PM

Posted 20 December 2014 - 02:49 PM

oops... I messed up in my explanation. You are supposed to add the numbers together to determine what octal to give.

read is 4

write is 2

execute is 1

 

So say you wanted the read and execute permission? The octal is 5 (4+1). You want execute and write? The octal is 3. You only want execute? It is 1 in that case. And if you don't want any permissions (only root can access) the octal is 0.


Edited by bmike1, 20 December 2014 - 02:55 PM.

A/V Software? I don't need A/V software. I've run Linux since '98 w/o A/V software and have never had a virus. I never even had a firewall until '01 when I began to get routers with firewalls pre installed. With Linux if a vulnerability is detected a fix is quickly found and then upon your next update the vulnerability is patched.  If you must worry about viruses  on a Linux system only worry about them in the sense that you can infect a windows user. I recommend Linux Mint or, if you need a lighter weight operating system that fits on a cd, MX14 or AntiX.


#15 technonymous

technonymous

  • Members
  • 2,474 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:08:12 PM

Posted 22 December 2014 - 02:17 AM

Not sure if you wanted to use root, but It apears that you are using the root cron. It shows mailto=root You can login as your user and edit your own crontab crontab -e

 

 

MAIL="bob"
MAILTO="bob@google.com

0 2 1 * 0 /home/bob/full-backup
0 2 * * * /home/bob/bobsdailyscript.sh
0 2 * * * echo "Happy Holidays!" > /etc/motd






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users