can not run cron job

Hi all

I set up a job using crontab but the job was not executed at all. I doubt it is because of user privilege. So I changed to root user and reinstalled the crontab job but it was still not executed. I dont have cron/cron.allow or cron/cron.deny at /usr/lib. is this the reason? How can I solve this problem?

BTW, I was able to set up a crontab at a Mac labtop. now I am working on a MAC OS X server. the version of the OS is 10.4.10.

thanks alot

Update

I did research and it says that if both cron.allow and cron.deny doesnt exist, I should be super user to run crontab job. How can I be a super user? my account is the administrator of the server.

today I did a test. I was able to set up a cron job which create a directory locally. however, If I wanted to create a directory remotely in windows XP, I could do it manually but not with cron schedule task. The script is below


set shareUser to "rimageservices" --area to change
set sharePass to "password" --area to change
set shareLoc to "10.1.2.134/archive" --area to change
set mountDir to "archive"
--
set volumesDir to ":users:load:desktop:" & mountDir
set cmdDir to "/users/load/desktop/" & mountDir
--
set jobFolder to (("Users:load:Desktop:") & "Jobtest:") -- path to desktop points to desktop folder of the current user
--

--//check if the connection to the remote location is available or not
set outp to (do shell script "ping -c 1 10.1.2.134 | grep '0% packet loss'") --area to change

if (outp = "1 packets transmitted, 1 packets received, 0% packet loss") then
	
	-- // check if the mapped drive is available or not
	try
		alias (volumesDir) -- // do nothing if it exists
	on error
		-- // create the file if it does not exist
		do shell script ("mkdir -p " & cmdDir)
	end try
	
	try
		do shell script ("mount_smbfs //" & shareUser & ":" & sharePass & "@" & shareLoc & " " & cmdDir)
		--display dialog cmdDir & " is mounted."
		
	end try
	
	do shell script "mkdir " & cmdDir & "/test"
	
else
	error number -128
end if


I set this cron job with either current user or root user but still didnt run. Can someone please help me?

Maybe you are getting an error? The cron daemon sends all output from any cron jobs to the system email account for the owner of the cron job. Have you checked for system email (/var/mail/username)?

At a bare minimum, you can use the command-line cat program to display the contents of the system mailbox. Or you can open the mailbox with any text editor (try UTF-8 encoding if you see encoding problems). If the messages have any transfer encoding though you might have trouble reading the messages.

From the command-line, you can use the mail program to read and delete any messages there (p to display a message, d to delete the current message, q to quit and save changes (deletions)).

Or you can use Mail (aka Mail.app) to import the messages. In my version of mail (Version 2.1.2 (753)) from Mac OS X 10.4.11: click menu item Import Mailboxes. from the File menu, select the Other radio item, click the Continue button, type Command-G, type /var/mail, click the Go button, click the Choose button, make sure your short username is checked, click the Continue button. If you do not like Mail, other email programs should be able to import these mailboxes too (they are in “mbox” format).

If you are using a method that cannot delete messages (cat, a text editor or importing), you will probably want to delete the old messages at some point. The command line cp /dev/null /var/mail/username will empty the specified system mailbox.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)

thanks crys but I am not setting up a cron job with mail item. All I want to do is able to set up a schedule to run that script, which will create a folder in mapped drive.

First, to be clear, I was writing about local, system-based email (/var/mail/* mbox files and Postfix) not Internet-based email (POP, IMAP, SMTP, etc.). What I was talking about has nothing to do with sending an email out to an Internet email address. If the command that you tell cron to run has any output (to UNIX stdout or stderr), it will be captured in a system-based email (unless you turn it off by setting MAILTO=“” in your crontab). I was hoping you would find an error message that would help explain why your script is failing.

Do other commands in the crontab work?
(put something simple like * * * * * echo “testing cron” ” this will generate a “system mail” if it runs)
What command line are you using in your crontab to launch the script?
(osascript, or are you saving the script as an app and running that?)
What happens if you run your script from a Terminal window (use the same command line you have in the crontab)?
Does it create the directory on the server?
Is there any output?

Alternatively, if you cannot get cron to work , you might try launchd.

In crontab I use “osascript /path/script.script.” I can successfully run this command in terminal window. The test script is to create a directory in mapped dive to windows. I just dont understand that why I can do it in terminal window but not with crontab.

The other point is when I run this script in a MAC labtop, which also has a mapped drive to windows, I could run this script successfully.

I tried * * * * * echo “testing cron” but there is nothing generated in /var/mail/

I see your point now. everytime I open a new terminal shell, I dont see a message " you have a new mail". Meanwhile there is no item in /var/mail.

So How can I turn on this system mail ?

Please confirm for me if these are true:
You launch your AppleScript program with osascript /path/to/script.scpt
Your script works when launched (as above) from a Terminal window on both Mac OS X 10.4 and Mac OS X Server 10.4.
Your script works when launched (as above) from cron on Mac OS X 10.4, but NOT Mac OS X Server 10.4.
A simple crontab entry like * * * * * echo “testing cron” does not produce any system-email output (in /var/mail) on Mac OS X Server 10.4.

Upon further consideration, a better diagnostic crontab might be something like this:[code]SHELL=/bin/sh

          • /usr/bin/env > /tmp/cron-env.txt 2>&1[/code]
            If (after a minute or so) you do not get a file at /tmp/cron-env.txt, then you can be pretty sure that cron is not picking up and running the entry from your crontab. This diagnostic crontab makes very few assumptions (the /bin/sh file exists, is executable by the user, and is a Bourne-type shell; the /usr/bin/env file exists and is executable by the user; the /tmp directory exists and is writable by the user; cron works).

If you do not get a result from this diagnostic, assumption reduced crontab, then it looks like cron is either not running or is corrupted. To check for cron running, type ps -auxww | grep [c]ron in a Terminal window. If it does show an entry for /usr/sbin/cron then something fairly fundamental is wrong. It could be a corrupted OS installation, hard to say what exactly the problem is. If the command does not show an entry for /usr/sbin/cron, then cron is not running. You will have to investigate the configuration of launchd to try to determine why (launchd starts almost all background processes as well as the main GUI).

To check out launchd run sudo launchctl list | grep cron in a Terminal window as an admin user. It should show an entry for com.vix.cron. If it is there you can try restarting cron by running sudo launchctl stop com.vix.cron;sudo launchctl start com.vix.cron. If it did not show com.vix.cron then try loading it by running sudo launchctl load -w /System/Library/LaunchDaemons/com.vix.cron.plist. You will not see any output, but you should see cron running now if you check the process listing again (ps -auxww as above). If you see nothing found to load when doing the launchctl load then either the launchd config file for cron is missing or there is something wrong in the config file.

Once you get cron running, start the diagnostics over again with the env crontab. If that works, try your own crontab entry that uses osascript. You may need the full path to osascript (/usr/bin/osascript). If the diagnostic crontab entry works but your osascript crontab entry does not seem to work, then check for system-based mail in /var/mail. If there is none, you can bypass cron’s collection of the output by capturing it yourself like is done in the diagnostic crontab entry (> logfilepath 2>&1).

As far as /var/cron/allow and /var/cron/deny go, if a user can successfully install or update a crontab using the crontab command then the user is allowed to use cron.

Also, for the system-based email stuff, it is not terribly important that cron is able to use it, it is usually just a convenient way of gathering the output from the crontab commands. You can do your own output collection using redirection as shown in the diagnostic crontab example earlier in this post. Keep in mind though that in using files in /tmp (or other world-writable directory) with predictable filenames has serious security implications. Files with predictable names should only be created in directories where the user is the only one with write permission. Or filenames with random elements should be used (mktemp can help with that). It is usually OK to use predictable file pathnames in testing, but they should be avoided when “in production”.

I used your method to check if cron job is running. It’s positive. Then I restart it and run the diagnostics and it works. but just right after I use my own crontab entry, which is “/usr/bin/osascript …”, nothing happened. It seems my cron is working and the script is working as well. once I combine them, I get nothing.

I think I find out the problem. there is a line in my script which uses “ping” command but it seems crontab can not recogonize it. The original script is below


set shareUser to "administrator" --area to change
set sharePass to "password" --area to change
set shareLoc to "10.11.22.115/Archive" --area to change
set mountDir to "Archive"
--
set volumesDir to ":users:adminsitrator:desktop:" & mountDir
set cmdDir to "/users/administrator/desktop/" & mountDir
set transDir to "/users/administrator/desktop/job/transfer/"
--set archivedJobs to "users/load/desktop/ArchivedJobs"
--set tempArchivedJobs to "volumes/backup 2/TempArchivedJobs"
--
set jobFolder to (("Users:administrator:Desktop:") & "Job:") -- path to desktop points to desktop folder of the current user


--//check if the connection to the remote location is available or not
set outp to (do shell script "ping -c 1 10.11.22.115 | grep '0% packet loss'") --area to change

if (outp = "1 packets transmitted, 1 packets received, 0% packet loss") then
	
	-- // check if the mapped drive is available or not
	try
		alias (volumesDir) -- // do nothing if it exists
	on error
		-- // create the file if it does not exist
		do shell script ("mkdir -p " & cmdDir)
	end try
	
	try
		do shell script ("mount_smbfs //" & shareUser & ":" & sharePass & "@" & shareLoc & " " & cmdDir)
		--display dialog cmdDir & " is mounted."
		
	end try
	
	-- //aumatically move folders
	set sFolders to paragraphs of (do shell script "find " & quoted form of POSIX path of jobFolder & " -type d -mtime +90 -maxdepth 1")
	
	set JobSize to 0
	set TotalSize to 200
	
	set tempTime to (do shell script "date '+%H_%M_%S_of_%m_%d_%y'")
	set package to (do shell script "mkdir -v " & quoted form of transDir & "job_" & tempTime)
	
	if (sFolders is not equal to {""}) then
		
		repeat with oneFolder in sFolders
			
			
			set jobcontent to paragraphs of (do shell script "find " & quoted form of POSIX path of oneFolder & " -mindepth 1 -maxdepth 1")
			
			
			if (jobcontent is not equal to {""}) then
				
				repeat with singlecontent in jobcontent
					
					do shell script "/usr/bin/ditto -c -k -rsrc --keepParent " & quoted form of singlecontent & space & quoted form of (singlecontent & ".zip")
					do shell script "rm -R " & quoted form of singlecontent
					
				end repeat
				
			end if
			
			
			
			set tempSize to (do shell script "du -ks " & quoted form of oneFolder & "| cut -f1")
			set tempSize to (tempSize / 1024)
			
			set JobSize to JobSize + tempSize
			
			if (JobSize < TotalSize) then
				do shell script "mv " & quoted form of oneFolder & space & package
			else
				set tempSize to 0
				set JobSize to 0
				set tempTime to (do shell script "date '+%H_%M_%S_of_%m_%d_%y'")
				set package to (do shell script "mkdir -v " & quoted form of transDir & "job_" & tempTime)
				do shell script "mv " & quoted form of oneFolder & space & package
			end if
			
			
		end repeat
		
		set movingFolders to paragraphs of (do shell script "find " & quoted form of transDir & " -type d -maxdepth 1 -mindepth 1")
		
		if (movingFolders is not equal to {""}) then
			repeat with oneMoveFolder in movingFolders
				--do shell script "cp -pRP " & quoted form of oneFolder & space & tempArchivedJobs
				
				do shell script "mv " & quoted form of oneMoveFolder & space & cmdDir
			end repeat
		end if
		
	end if
else
	error number -128
end if



After I run this script in crontab, I got error message below:

Is there anyway to avoid this execution error because this “Ping …” line of script is important. If I get rid of this line, there will be a lot of trouble to handle the case that the connection is not available between Mac and Mapped machine ( which is windows XP).

Add

PATH=/usr/bin:/bin:/usr/sbin

to the top of your crontab. ping is in /usr/sbin which is not in the default PATH that cron gives to the programs it runs.

Just as a note, it looks like everything that your AppleScript program does could be done in a normal shell script, too.

yeah I got it fixed with your solution. simply added that path got everything running smoothly.

It’s my first apple script project and I feel it is easier to use apple script call shell command now. but later I will definately convert it to shell script.

Thanks for your help, I really appreciate.:smiley: