LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with Script and Cron (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-script-and-cron-182237/)

goldstonrumpke 05-16-2004 08:43 PM

Help with Script and Cron
 
I can run the script manually via the shell but I want to set up my Crontab to do this for me. I am tired of these tedious tasks and that is what Cron is for right? Please if anyone can help I would be grateful :)

Thanks in advance ...

/* this is my script */

#!/bin/bash
cp -u /home/ccs/* /var/ftp/ccs
exit

/* this is how I tried to update with crontab */

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

30 * * * * /bin/bash /root/scripts/update.sh
00 * * * * /bin/bash /root/scripts/update.sh

homey 05-16-2004 09:00 PM

I wonder if you would have better luck making it executable. Something like this.....

Make a file called /home/test
#!/bin/bash
cp -u /home/ccs/* /var/ftp/ccs
exit

Run these commands:
chmod +x /home/test
export EDITOR=vi
crontab /etc/crontab
crontab -e

Add a line to your personal crontab.....
30 * * * * /home/test

Save the changes with the command: :wq!

Don't forget to start the crond service.

pcardout 05-17-2004 02:04 AM

Help with Script and Cron
 
Oooh ooh I know -- cause I had this EXACT problem.

You have to say /bin/cp ... plain cp doesn't work. That's probably ALL
that's wrong. Here's some other info you didn't ask for.

Setup a cron job
crontab -urichard -l (to list) and crontab -urichard -e (to edit)
If crontab doesn't allow you to do this, put the name richard in
the file. /etc/cron.allow (create the file if you need to). You do not
need to stop and start cron ... at least not when using crontab, possibly
when creating the cron.allow file.


Here is a working crontab file
# Beginning of crontab file
SHELL=/bin/bash
# Select a shell to use
* * * * * /bin/echo "One minute has elapsed">>/home/richard/rscron.log
# Output this message every minute. Note that have to use a path
# explicitly for echo as the usual path is not what cron uses. Without the
# /bin, this script produces no output.

aeichner 05-18-2004 03:14 AM

Hmmm. If you _really_ want to put your Script in the global crontab, don't forget to set the username. Cron normally runs as root and also all the scripts it will run for you - this is bad security. So set the username, either "root" or better a non-privilleged user.

But I suggest to use the way described above. If it's normally the job of your user, then use a personal crontab to do the job.

goldstonrumpke 05-19-2004 09:41 PM

Thank you for all of the help ...
I tried this one ... crontab -uroot-l (to list) and crontab -uroot-e (to edit) , and the shell said "no crontab for user root" How would I make a crontab for the specific user? What is a global crontab and why is it bad? This is the one I am using right? Is it the file in the root file system? How could I limit to a specific user? I am so confused.

I will keep trying :)
Thanks in Advance .

goldstonrumpke 05-19-2004 09:43 PM

Yep, I also CHMODed the directory
 
I forgot to write that I also CHMODed the directory as indicated.

pcardout 05-19-2004 10:58 PM

I just tried the following on my system and got a result similar to yours. Here it is:

"garage:/etc# crontab -uroot -l
You (root) are not allowed to use this program (crontab)
See crontab(1) for more information".

Here's what I think is happening. Linux is trying to protect you from running crontab as root. It really wants you to run it as some other user. Here is how you do that:

I'll assume you have a user on your system called adam.

Then you edit /etc/cron.allow (creating it if it doesn't exist) and add
a line that simply says "adam". (No quotes). My /etc/cron.allow consists of a single line, "richard". That's it.

Then you can crontab -uadam -e. You will notice that you are dropped into an empty file, you aren't messing with that crontab "minute", "hour" day file. That's the one that aeichner called "global". Go look at the example crontab file I showed in my last post. I specifically had it write a message once a minute so I wouldn't have to wait too long to see if it was working! Once you've got that, you should be able to do whatever you want.

kenji1903 05-30-2004 09:34 PM

G'day!

Interesting post! ;)
Will's my username...
I had tried crontab -uwill -e and inserted your line in it, works like a charm :D

Then I thought, hey, why not try rebooting it and see whether cron will run...
I inserted:
00 10 * * * /sbin/reboot
# 10am is just my local time

Then i got this message: You have a new mail in /var/spool/mail/will

When I check the file, i got this: reboot: must be superuser

If i want to reboot my server daily at 2am in the morning, how do i set cron to do that?

Cheers!

fortezza 05-30-2004 11:10 PM

Reoobt permissions
 
do an "ls -l `which reboot'" ( without the double quotes, keep the backticks ) and make sure you have permission to run the reboot command. if you don't, then what I would do is use chmod to let you do that, and then try it from the command line to make sure it works. Another option for reboot is "shutdown -r now", which also requires that you have permission to run it.

kenji1903 05-30-2004 11:21 PM

Well, its owned by root alright...

Instead of changing the ownership of the file, is it possible to add my user "will" into the group root? is it recommended to do so?



do an "ls -l `which reboot'" ( without the double quotes, keep the backticks ) and make sure you have permission to run the reboot command. if you don't, then what I would do is use chmod to let you do that, and then try it from the command line to make sure it works. Another option for reboot is "shutdown -r now", which also requires that you have permission to run it.

kenji1903 05-30-2004 11:51 PM

hmm... whats the difference between:
/usr/bin/reboot
/sbin/reboot
?
Which one usually gets executed?


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