Replace a file with a Cron job?

8 replies
Hello,

I have to upload two files everyday for a promotion I'm doing. I have to do it at the same time everyday and it's quite annoying and not at the best time for me.

Anyway, I was wondering if there was a way to create a cron job so I would put my updated files in a folder and thanks to this cron job they would be transfered in the other directory to replace the "old" files?

Thanks to enlighten me.
#cron #file #job #replace
  • Profile picture of the author Tim Franklin
    Cron is not the best method, since it is not the most secure way to do this, I would use SFTP, this can be done using a scripting command, or you could purchase a software product,

    free, but complex,

    Senior Advisor - http://www.unix.com

    There are several software clients, they all seem to run around 85 bucks, not sure why, if you had a MAC you could just script it,

    Google, AbleFtp

    also you can try automate FTP or better sFTP
    Signature
    Bitcoin | Crypto | AI software tools and development|
    {{ DiscussionBoard.errors[2955251].message }}
  • Profile picture of the author electrobooks
    Hi David,

    Cron itself wont do the transfer but the script that the Cron job runs can. Do you have any programing experience (PHP etc) ?

    If so using the php file commands should work to copy the file content from one file and create a new one with it. fopen() fgets() fputs(). See php.net for more.

    Cheers

    Chris
    {{ DiscussionBoard.errors[2957203].message }}
  • Profile picture of the author kkoechel
    Originally Posted by david_g View Post

    Hello,

    I have to upload two files everyday for a promotion I'm doing. I have to do it at the same time everyday and it's quite annoying and not at the best time for me.

    Anyway, I was wondering if there was a way to create a cron job so I would put my updated files in a folder and thanks to this cron job they would be transfered in the other directory to replace the "old" files?

    Thanks to enlighten me.
    do you have access to scp and ssh for these servers?

    Here is the perl code that will do it:

    HTML Code:
    #!/usr/bin/perl
    #scp.pl
    #kkoechel.net
    
    use strict;
    use Net::SCP::Expect;
    
    my @files = qw(	index.cgi
    		work.jpg 
    		whatever.file
    		);
    my &remotelocation = '/path/to/some/directory';
    my &user = 'YOURUSERNAME';
    my &password = 'YOURPASSWORD';
    my &host = 'YOURHOST.senecamedia.com';
    my &timeout = 75;
    my &scpe = Net::SCP::Expect->new(timeout=>&timeout);
    &scpe->login(&user, &password);
    foreach my &file(@files){
    	print "sending &file to &host:~/&owner/&filen";
    	&scpe->scp(&file,"&host:&remotelocation") if &file;
    }
    you can run this script like this: perl scp.pl after you make the needed changes.

    Using cron or windows task scheduler is easy to google, and you should to make sure you have the right syntax

    EDIT: it keeps wiping out my variables, I am guessing some sort of javascript cleaner?

    EDIT: OK, I replaced all the dollar signs with ampersands, you need to find and replace those back in before this will work.
    {{ DiscussionBoard.errors[2957881].message }}
    • Profile picture of the author david_g
      Thanks to all of you for your advices and lines of code.

      I was expecting to be able to do that with a simple Cron job, bad luck for me.

      I don't want to spend any money for that as I just need to do this operation for 3 weeks.

      I also don't have any scp access, I'm just using a simple shared hosting account.

      Ok, I think I will continue to do it by hand.

      Thank you again.
      {{ DiscussionBoard.errors[2959208].message }}
      • Profile picture of the author kkoechel
        What kind of access do you have? I could rewrite that for just about any logon mechanism
        {{ DiscussionBoard.errors[2960411].message }}
        • Profile picture of the author david_g
          Originally Posted by kkoechel View Post

          What kind of access do you have? I could rewrite that for just about any logon mechanism
          I have an access with cPanel but this is not my own server so I don't have a root access or whatever. My rights are limited as I'm using a shared hosting account.
          {{ DiscussionBoard.errors[2961924].message }}
          • Profile picture of the author kkoechel
            Originally Posted by david_g View Post

            I have an access with cPanel but this is not my own server so I don't have a root access or whatever. My rights are limited as I'm using a shared hosting account.
            yeah, bummer my friend. by hand it is.
            {{ DiscussionBoard.errors[2962584].message }}

Trending Topics