4

I just start with cron jobs and I wanted to start with an easy task. Create a file in my home folder with the output of the ifconfig. So I created test.sh, which I made it executable with the:

#!/bin/bash          
ifconfig > /home/myname/ipt

inside. When I run it from terminal it creates the file ipt which has the output ifconfig.

To add the script in my crontab I entered:

sudo crontab -e

and I added my script like this:

* * * * * /home/myname/test.sh

and the first minute it creates the file ipt which has an lock icon which is for read only and inside is empty.

There are two questions:

1st Why it is empty?

2nd Why it is read only file?

4
  • 2
    Use the full path to ifconfig i.e. /sbin/ifconfig >/home/myname/ipt
    – heemayl
    Dec 17, 2015 at 19:40
  • 1
    Please pay attention to the format of /etc/crontab! You seem to be missing the column with the name of the user, under which the command shall be run. I'm surprised the editor crontab -e accepted your changes despite the illegal syntax. Dec 17, 2015 at 21:01
  • @DavidFoerster you don't need the name of the user in user's own crontab i.e. crontab -e ....you need user's name in the generic ones i.e. /etc/crontab, /etc/cron.d/*
    – heemayl
    Dec 17, 2015 at 21:06
  • Also, you should use a >> as opposed to a > if you want to include previous entries, as > will delete whatever is currently there before writing.
    – user323419
    Dec 17, 2015 at 21:11

2 Answers 2

2

Please try what @heemayl said, use full path within your test.sh:

/sbin/ifconfig >/home/myname/ipt

Explanation

On command prompt and running scripts, you are doing so as your user which has certain capabilities, such as

  • having $PATH variable include /sbin/ to be able to "know" whenever asking for ifconfig refers to /sbin/ifconfig

Cron however is not the same exact user as you

  • thus when cron tries to run, it doesn't know where ifconfig is
  • unless of course, you specify exactly where: /sbin/ifconfig

The effect of not knowing where a program is, you can test for yourself, try running a program that does not exist on your command prompt:

$ doesnotexist > ipt2
bash: doesnotexist: command not found

Notice when you:

$ ls -lh ipt2
-rw-r--r-- 1 youruser youruser 0 Dec 17 14:48 ipt2

It is a

  • 0 byte file
  • read-only

This is exactly the same result you originally found with crontab, where it did not know what ifconfig is, resulting in making a 0 byte, mostly read-only file.

So in cron, help it find the program by just giving it the full path, and as long as you have no permission issues, it should work.

1

Answer no 1:

The file was empty because I didn't give the full name of the command thanks to @heemayl and @user454038

Answer no 2:

The file was read only because I created a cronjob as root and I was trying to open it as user

sudo crontab -e

but I just had to created as user without sudo

crontab -e

So working like user the .sh file would be

/sbin/ifconfig > ipt

and the crontab

* * * * * $HOME/test.sh

and I use the variable $HOME for my user again.

So this creates the file that I wanted without wrong permissions.

Thank you all for your help.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .