Results 1 to 6 of 6

Thread: how to get cron (root) to behave as if logged in?

  1. #1

    Default how to get cron (root) to behave as if logged in?

    When logged in as 'chuck' I can # cd / to get to my root directory. Next I can run an rsync command to backup files.

    When I try to set the same thing up as a cron job under root, my files are not being backed up. I believe it is because the starting directory location is wrong.

    So, when executing a script or a command under cron as root, how do you specify the directory statement so it starts in the same spot as if you are logged in as a user and do a #cd / command?

    Yes, you can tell I'm a newbie and not familiar with how the linux directory structure works...thanks...

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: how to get cron (root) to behave as if logged in?

    root's $HOME is not /. It is actually /root. So a root cron job would start in /root

    Nonetheless, it's wiser not to depend on the starting directory when you run a cron job. You can simply do a cd / in the cron command.

    1 1 * * * cd /; rsync ...

  3. #3

    Default Re: how to get cron (root) to behave as if logged in?

    ok, thanks!

    So...just to make sure I understand: if I am logged in as root and I do a cd / then that gets me to the same starting directory as if I were logged in as chuck and I do a cd / (?)

  4. #4
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: how to get cron (root) to behave as if logged in?

    When you do a cd / your current directory becomes / no matter who you are, even the pope.

    (Well, permissions permitting, but a system where you couldn't cd to / would be unusable.)

  5. #5
    ab@novell.com NNTP User

    Default Re: how to get cron (root) to behave as if logged in?

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Exactly. To go along with this there are two options when changing
    directories.... absolute paths and relative paths. Relative paths depend
    on where you are currently and absolute paths depend nothing. Having
    fewer dependencies typically makes your code run more-smoothly.

    cd /

    That is an absolute path because it starts with '/'. There is only one
    '/' and since you start there anything you put after it (including
    nothing) still is an absolute path. A few relative path examples:

    cd ./
    cd ./let/us/change/to/here
    cd let/us/change/to/here
    cd ../../couple/levels/up/then/here

    These all depend on you knowing where you start. That's fine as long as
    you do, but I always try to start knowing where I start with an absolute
    path because it reduces problems significantly.

    In your case no matter who runs `cd /` they should get to the root of the
    filesystem and, as ken yap mentioned, if they cannot then you have much
    bigger problems.

    Good luck.




    ken yap wrote:
    > When you do a *cd /* your current directory becomes / no matter who you
    > are, even the pope.
    >
    > (Well, permissions permitting, but a system where you couldn't cd to /
    > would be unusable.)
    >
    >

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iQIcBAEBAgAGBQJJziPVAAoJEF+XTK08PnB5iHgP/AhNCSb+qqFlgd2oqtJi2gXT
    6pc4lNKjiW0JtBgwo761eO1nGRptTHjaQZhtBt5GmyFQA960YZREN0lIYxIRGlkR
    uOpPRC0SZEWeqrspM5P3be5GmTxFZm0JRcDkFxrK+6ScS55eGILzRF46+zuZHk53
    mxmLeTiMV63O6MXqu3A13qKyTohMNXu2uWcuGIXsTvQr2tpy7MkgOXzir6elo5uL
    UdxbwuiFn27ND6Xi+XIqk5xr/uAbI2iQa21BRKxzDCn2AYOoIk5H4Rb5RmfpPnln
    PNREuNGRA4A8c5Y6M++crF4qi6c/AK03/gMZZs513EIcCJb9hExTYaKhhjhUBk5w
    ORmtHZeZoMfe5MXUvFuV3sTM4gTendR0Y3bh7BeD7yCvEUTcD8N1QtdPGc+qqVHJ
    1+o16HEeZUGKR7C8pgVKO5TZ1uYSOtx0Vv4oAs0xKZANE3yyk0aSGWD4uhA+BEfY
    kXkfyjGNZYkNeLc3j/HdepXKIfOEUW22BzoQhZlsMO+9hHiVo9VboqrsZjueu2tM
    TmN0fz0c4qvI4Lej6fmegYWvf6B7hwtK6hmV9Blalc9brwW4znoDvtQM+7NJROlY
    MGoJQqqL4W1xYR5ubqHedy2kiAj3BKXxF7/vM8UtXtRwJXz+MwyGJG9Ycl4PAj73
    6TGut3XUjTtA/uo6iDjj
    =c8NF
    -----END PGP SIGNATURE-----

  6. #6

    Default Re: how to get cron (root) to behave as if logged in?

    Thanks so much for both responses. I understand much better now -- and my little cron job worked perfectly last night.

    -C

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •