0

My Question has 2 parts. Help with both parts is greatly appreciated.

GIVEN INFO: - Lets assume there are 2 Folders, Folder A and Folder B. - Folder A contains many files. - Folder B contains the Shell script (code.sh)

PART A: The script code.sh has to delete all the files in folder A and also afterwards delete the Folder B Including the script code.sh

PART B: Also this has to be scheduled via crontab at some particular time which in itself is not a problem. But after the script has deleted contents of Folder A, it should remove its entry from crontab and then proceed with deleting Folder B as mentioned in PART A.

Is the above possible? If so can someone please tell me how or atleast point me in the right Direction. I'm new to UNIX so any help would be greatly appreciated.

Note: In the whole process, only scheduling the script (code.sh) in crontab should be manual.

3
  • 4
    Any particular reason you want to use crond and not atd/at which is specifically designed to run things once at a specified time? Oct 21, 2014 at 11:21
  • @UlrichSchwarz thanks for that. I think i can try the at command which looks good enough for what im doing. I wasn't aware that such a command existed. Any clue about the first part of the question?
    – rapidclock
    Oct 21, 2014 at 12:05
  • 3
    Just use rm -rf FolderA FolderB. There's no problem with deleting the script itself. On Unix, the contents of a file aren't actually removed until the last process that has it open closes it.
    – Barmar
    Oct 21, 2014 at 19:06

2 Answers 2

1

If this script is ran as root...then removing the entry from crontab can be done by removing its crontab line from /var/spool/cron/root. (This is what crontab -l and crontab -e use).

At the end of your delete commands add the following line (substituting {pattern} for your script name):

sed -i '/{pattern}/d' /var/spool/cron/root

So if your entry in crontab that runs code.sh looks like

0 21 * * * /etc/cron.daily/code.sh

The command would be:

sed -i '/code.sh/d' /var/spool/cron/root

This will remove the line containing "code.sh" from your root crontab file.

You can add the line above to the end of your script so that both files are deleted, and the entry is removed from crontab. As @Barmar stated, you can just delete both folders aread of time.

Yes this is a very old question, but the direct question on removing from crontab specifically still needed an answer.

0

As long as the script has permission to delete the files, there is absolutely no problem with this. On unix-based systems, there are no restrictions for a script to alter or delete its source file. The filesystem structure is handled so that the physical storage associated with the file is freed only when all the processes close the file (after deletion the file will no longer be visible in the directory structure, but the process that still holds the inode that points to the file data can access it: only when nobody remembers the inode, the file is lost).

Unless of course the interpreter that is reading the script wants to close and reopen the file. Bash shouldn't be a problem.

Also, this looks like a one-time job. Don't use crontab. Use at or even just sleep amount && ./script which is usually the quick-and-dirty way of doing this (but won't work if the machine is restarted or the terminal is closed).

7
  • While it's possible to remove the file from the directory listing, It's not possible to create a new file with the same name while it's still open / running. So there can't be no problem with that as it would be possible to detect the self-deletion by trying to create a file with the same name.
    – hakre
    Aug 16, 2015 at 15:29
  • @hakre Of course you can create a new file with the same name. The name being similar is not important, it will point to a new inode that has nothing to do with the old one.
    – orion
    Aug 17, 2015 at 7:46
  • i have problems with a script that - on git checkout - get's deleted and then re-created. this operation does not work. deletion is done, creation does not work (error on stat IIRC).
    – hakre
    Aug 17, 2015 at 9:06
  • I tested it and creation of a file that shares a name with a deleted file definitely works on inode-based filesystems (tested both a running bash script and a file explicitly open for writing in a short test c program). What filesystem are you using? Also, how is the file in use (running script, open in text editor,...?). Some programs may explicitly lock a file, but even then, it should fail on deletion, not creation. Is there a permission problem? What is the exact error you get?
    – orion
    Aug 17, 2015 at 9:27
  • file system is NTFS. bash is git bash on windows. stat fails on git checkout.
    – hakre
    Aug 17, 2015 at 10:00

You must log in to answer this question.

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