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 job does not work.


  • Please log in to reply
7 replies to this topic

#1 BlueGalaxy

BlueGalaxy

  • Members
  • 83 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:04:25 PM

Posted 13 March 2017 - 11:49 PM

Hello guys! I am new to crontab. I now created a simple crontab job. It should execute every 15 minutes to alert me that I need to take a short break. However, it does not work. I don't know why. Please help me if you have experience with crontab. I used the command crontab -e to create this file and save it.

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
*/15 * * * * /home/galaxy/Documents/projects/Bash/cron_job.sh

This file is supposed to execute the script cron_job.sh every 15 minutes. This is a Bash shell script that has a chmod value of 500. It contains one command to create a new gnome-terminal and immediately run a command inside that new terminal to cat the file take_a_break. That is a text file with a simple message telling me to take a break. Either running the shell script or just running the command contained inside of it works flawlessly, but the crontab job does not work at all. Maybe I formatted something incorrectly. Here is the contents of the file cron_job.sh;

#!/bin/bash

gnome-terminal -e "bash -c \"cat $HOME/Documents/ascii/take_a_break; exec bash\""




BC AdBot (Login to Remove)

 


#2 DeimosChaos

DeimosChaos

  • BC Advisor
  • 1,421 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:United States, Delaware
  • Local time:05:25 PM

Posted 13 March 2017 - 11:52 PM

I guarantee cron has no idea what $HOME is. Apparently its finicky with environment variables. Try just using the full path with out that and see if it works.

 

*Edit

 

Oh, and also makes sure you have full paths in your script if you use them in there. Even if you have your script in a directory and call something in that directory, it won't be able to find it.


Edited by DeimosChaos, 13 March 2017 - 11:54 PM.

OS - Ubuntu 14.04/16.04 & Windows 10
Custom Desktop PC / Lenovo Y580 / Sager NP8258 / Dell XPS 13 (9350)
_____________________________________________________
Bachelor of Science in Computing Security from Drexel University
Security +


#3 BlueGalaxy

BlueGalaxy
  • Topic Starter

  • Members
  • 83 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:04:25 PM

Posted 14 March 2017 - 12:08 AM

I guarantee cron has no idea what $HOME is. Apparently its finicky with environment variables. Try just using the full path with out that and see if it works.

 

*Edit

 

Oh, and also makes sure you have full paths in your script if you use them in there. Even if you have your script in a directory and call something in that directory, it won't be able to find it.

 

But that command is executed in a script. The script works perfectly well without the need to change $HOME to the full directory path. I think of it as a form of encapsulation. Corntab does not necessarily need to know what commands are inside the script, am I right? Anyway, I changed it and the script now looks like this:

#!/bin/bash

gnome-terminal -e "bash -c \"cat /home/galaxy/Documents/ascii/take_a_break; exec bash\""




#4 wizardfromoz

wizardfromoz

  • Banned
  • 2,799 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:08:25 AM

Posted 14 March 2017 - 01:25 AM

Hi BlueGalaxy

 

If you get the opportunity, let us know how the revised version went?

 

I am working currently on alternative solutions between cron, anacron, /etc/rc.local etc, for running a script that I might use at startup, shutdown, or at periodic intervals.

 

Obviously, correct syntax between solutions is important to consider, so I am all ears.

 

Thanks

 

:wizardball: Wizard



#5 BlueGalaxy

BlueGalaxy
  • Topic Starter

  • Members
  • 83 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:04:25 PM

Posted 15 March 2017 - 08:38 PM

I still can't get the crontab job to work, even in the revised format.

http://www.unixgeeks.org/security/newbie/unix/cron-1.html

 

From this website, I used the following command to find out if crond was running, and indeed I got a line of output.

ps aux | grep crond

However I wanted to start crond explicitly, and the following command did not work.

$ crond
No command 'crond' found, did you mean:
 Command 'cron' from package 'cron' (main)
crond: command not found

Also, this part does not make sense. Why would the output get mailed? I just want the command to run and see the output on the screen.

Output from cron

As I've said before, the output from cron gets mailed to the owner of the
process, or the person specified in the MAILTO variable, but what if you
don't want that? If you want to mail the output to someone else, you can
just pipe the output to the command mail.


#6 wizardfromoz

wizardfromoz

  • Banned
  • 2,799 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:08:25 AM

Posted 15 March 2017 - 11:51 PM

Hi BlueGalaxy, I can answer parts of your queries, but someone more experienced may do better.

 

My comments, highlighted, between yours

 

 

 

I still can't get the crontab job to work, even in the revised format.

http://www.unixgeeks.org/security/newbie/unix/cron-1.html

 

From this website, I used the following command to find out if crond was running, and indeed I got a line of output.

ps aux | grep crond

DO be aware that the article you are referencing is over sixteen (16) years old?? A lot has changed since then.

 

The command you tried produced output, but then so does this one:

 

chris@Peppermint7 ~ $     ps aux | grep croncrap
chris     4869  0.0  0.0  14228  1020 pts/2    S+   14:27   0:00 grep --colour=auto croncrap

 

You see what I mean?

 

Nowadays, the cron daemon is most closely associated with cron.d

 

 

 

However I wanted to start crond explicitly, and the following command did not work.

$ crond
No command 'crond' found, did you mean:
 Command 'cron' from package 'cron' (main)
crond: command not found

No such command, at least not nowadays.

 

Also, this part does not make sense. Why would the output get mailed? I just want the command to run and see the output on the screen.

Output from cron

As I've said before, the output from cron gets mailed to the owner of the
process, or the person specified in the MAILTO variable, but what if you
don't want that? If you want to mail the output to someone else, you can
just pipe the output to the command mail.

 

 

In the ancient article you referenced, a glimpse of the answer is provided, where it says, in part:

 

"It is normally used for sys admin commands,..."

 

 

 

Regarding that last comment of mine, I will expand a little.

 

There is cron and there is anacron.

 

Cron, historically, has been used by System Administrators (sysadmins) whom might be administrating dozens of computers (nodes) over a Network incorporating one or more Servers. It is tailor-made to afford the opportunity to run housekeeping tasks at scheduled times such as hourly, daily and weekly. It can have application, nowadays, by the home user such as yourself.

 

Anacron is more commonly used, historically, by home users, for running tasks that coincide with events at ad hoc intervals, eg startup, shutdown, &c.

 

The lines between the two can become greyed.

 

As for the "mail" reference:

 

Sysadmins using cron can set up an email address for Root, which they then incorporate into the cron process.

 

When the cronjobs are performed, cron emails the sysadmin with the details, I am not sure if in the form of output appended to a log, or similar.

 

 

 

Hope this helps rather than hinders.

 

Apologies to the experts if my explanation is clumsy or flawed, just happened to be passing by, and now I'll disappear in a puff of smoke.

 

:wizardball: Wizard



#7 DeimosChaos

DeimosChaos

  • BC Advisor
  • 1,421 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:United States, Delaware
  • Local time:05:25 PM

Posted 16 March 2017 - 08:39 AM

Hi BlueGalaxy,

 

'crond' is the daemon. You don't use that to start it. I'm not sure what Linux you are using, but if its a Ubuntu/Debian variant try:

sudo service cron start

Also take a look here. Hopefully something there helps you get it working. I recently had to get a cron job working for work. It took a little bit to figure out. Its finicky apparently.


OS - Ubuntu 14.04/16.04 & Windows 10
Custom Desktop PC / Lenovo Y580 / Sager NP8258 / Dell XPS 13 (9350)
_____________________________________________________
Bachelor of Science in Computing Security from Drexel University
Security +


#8 wizardfromoz

wizardfromoz

  • Banned
  • 2,799 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:08:25 AM

Posted 16 March 2017 - 04:54 PM

That looks like a great reference, DC :thumbup2:

 

Thanks for sharing, I am bookmarking it.

 

:wizardball: Wizard






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users